C++的特殊成员

const成员

class person
{
public:
	void printc() const
	{
		cout << "hi" << endl;
	}

	int m_a;
};

如上,类中的函数后加了const修饰便为const的成员函数,若加了const修饰则该函数里面不能对值进行修改。若重复名,普通对象默认调用普通函数无法调用const函数,若只有const函数则普通对象可以调用const函数。const对象不能调用普通函数只能调用const修饰的函数

ststic对象

class person
{
public:
	static void  printc()
	{
		cout << "hi" << endl;
	}
	
	int m_a;
};

如上,加了ststic便是ststic函数,这个函数不属于类,类外可以直接调用不需要用对象

ststic int a;

也可以用ststic修饰一个变量,该变量只会在程序结束的时候释放

friend对象

class MM
{
	friend class person;
protected:
	void printmm()
	{
		cout << "mm" << endl;
	}
};

class person
{
public:
	void printpe()
	{
		MM m1;
		m1.printmm();
	}
	
private:
	int m_b;
};

如上,用firend申明person为MM的友元,person内就可以访问私有和保护权限

this指针:

谁调用,this便指向谁

class MM
{
public:
	MM(int a)
	{
		this->m_a = a;
	}
	int m_a;
};

若p1调用则这个this指向p1,p2调用就指向p2,this的地址由调用者决定
explicit:

class MM
{
public:
	explicit MM(int a)
	{
		this->m_a = a;
	}
	int m_a;
};

用exolicit修饰构造函数可以防止隐式类型转化

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值