explicit 显式转换 用法 没搞清楚了。

#include <iostream>
using namespace std;
class test1
{
private:
	int x;
public:
	 explicit test1 ( int num)
	{
		x = num;
	}
		void print()
	{
		cout<<x<<endl;
	}
	test1 operator << ( test1 p );

};
test1 test1::operator << ( test1 p)
{
	cout<<p.x <<endl;
	*this = p;
	return p ;
}
class test2
{
private:
	int y;
public:
	test2 ( int num = 0)
	{
		y = num;
	}
	void print()
	{
		cout<<y<<endl;
	}
};
int main()
{
	test1 p( 8 );
	p.print();
	test2 q;		// 可以隐式 调用构造函数
	q = 9;	 
	//p = 18; 


//	test1 p4;					p4   不能隐式调用  构造函数
	test1 p3 (99);				// explicit    p3 不能隐式调用 构造函数 , 只能显式 调用;
	q.print();
	p3.print ();
	p3<<p;			//重载 <<运算符      输出 p.x   返回 p     为 p3 赋值
	p3.print ();
	/*---------------------------------------
	2013年10月9日 19:51:57
	explicit  用法还是 很蛋疼,
	看着看着 就想起来了  运算符 重载。。。   
	记不住啊,  有点混乱。。
	------------------------------------------------------------------*/
	
	
	

	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值