65,211
社区成员
发帖
与我相关
我的任务
分享class INT32
{
private:
int INT;
friend ostream& operator << (ostream& out , INT32 a);
public:
INT32(INT32& a) {}
INT32(int a):INT(a) {}
INT32() {}
};
inline
ostream& operator << (ostream& out , INT32 a)
{
out << a.INT ;
return out;
}
int main()
{
INT32 a=2,b=3;
cout << a ; // 我监视过在输出的时候 a=3, 在输出后 a=2. 也就是说a只在输出时变过,之后又变回来了
system("pause");
}
INT32 a=2;//这句先调用INT32(int a)构造个临时对象,然后调用拷贝构造函数把这个临时对象作参数,
//默认的拷贝构造函数会初始化a::INT=2,但是现在你定义了后并没有初始化INT;所以INT是个垃圾值。
//这样也行
#include <iostream>
using namespace std;
class INT32
{
private:
int INT;
friend ostream& operator << (ostream& out , INT32 a);
public:
INT32(INT32& a):INT(a.INT) {}///这里拷贝构造函数要么用默认的,要么定义正确
INT32(int a):INT(a) { }
INT32() {}
};
inline
ostream& operator << (ostream& out , INT32 a)
{
out << a.INT ;
return out;
}
int main()
{
INT32 a=2,b=3;
cout << a ;
cout<<a;// 我监视过在输出的时候 a=3, 在输出后 a=2. 也就是说a只在输出时变过,之后又变回来了
system("pause");
}
#include <iostream>
using namespace std;
class INT32
{
private:
int INT;
friend ostream& operator << (ostream& out , INT32 a);
public:
/*INT32(INT32& a) {}*///这里拷贝构造函数要么用默认的,要么定义正确
INT32(int a):INT(a) {}
INT32() {}
};
inline
ostream& operator << (ostream& out , INT32 a)
{
out << a.INT ;
return out;
}
int main()
{
INT32 a=2,b=3;
cout << a ;
cout<<a;// 我监视过在输出的时候 a=3, 在输出后 a=2. 也就是说a只在输出时变过,之后又变回来了
system("pause");
}