#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;
}
explicit 显式转换 用法 没搞清楚了。
最新推荐文章于 2021-03-31 12:30:18 发布