#include <iostream>
using namespace std;
class test{
public:
test(){
cout << "constructor" << endl;
}
test(const test& a)
{
cout << "copy constructor" << endl;
}
~test(){}
test& operator = (const test& a)
{
if(this != &a){
cout << "=operator" << endl;
return *this;
}
}
};
int main()
{
test a; //constructor
test b(a); // copy constructor
test c = test(a); // copy constructor
test d = a; // copy constructor
test* e = new test(a); // copy constructor
test f; //constructor
f = a; // = operator overload
}
运行结果:

本文深入探讨了C++中拷贝构造函数和赋值运算符的使用场景与实现机制,通过具体代码示例展示了它们在实例化对象时的作用,包括拷贝构造函数与赋值运算符的调用时机及区别。
511

被折叠的 条评论
为什么被折叠?



