64,648
社区成员
发帖
与我相关
我的任务
分享
class B
{
public:
B(){
cout << "构造" << endl;
}
~B(){ cout << "析构" << endl; }
B(const B&){ cout << "拷贝" << endl; }
B(int i) :data(i){ cout << "参数构造" << endl; }
protected:
private:
int data;
};
B play(B b)
{
return b;
}
int main() {
B b(B());
return 0;
}
class B
{
int data;
public:
B(){ puts("构造"); }
~B(){ puts("析构"); }
B(const B&){ puts("拷贝"); }
B(int i) :data(i){ puts("参数"); }
B&operator=(const B&)
{
puts("赋值");
return *this;
}
};
int main()
{
B b = B();
}
重载了赋值,但结果还是一样的,没有出现赋值。。。[/quote]
B b = B();调用构造函数和拷贝构造函数, 但是编译器优化掉了
你可以把拷贝构造函数设置为private, 能发现是无法编译的.class B
{
int data;
public:
B(){ puts("构造"); }
~B(){ puts("析构"); }
B(const B&){ puts("拷贝"); }
B(int i) :data(i){ puts("参数"); }
B&operator=(const B&)
{
puts("赋值");
return *this;
}
};
int main()
{
B b = B();
}
重载了赋值,但结果还是一样的,没有出现赋值。。。