classInt { private: int value; public: Int() :value(0) { std::cout << "Default create Int" << "at" << this << std::endl; } Int(int v) :value(v) { std::cout << "Create Int with value " << value << " at " << this << std::endl; } ~Int() { std::cout << "Destroy Int with value " << value << " at " << this << std::endl; } };
intmain(){ Int a(10); Int b(20); Int c(b); //拷贝构造 return0; }
classInt { private: int value; public: Int() :value(0) { std::cout << "Default create Int" << "at" << this << std::endl; } Int(int v) :value(v) { std::cout << "Create Int with value " << value << " at " << this << std::endl; } Int(Int& it) :value(it.value) { std::cout << "Copy create Int with value " << value << " at " << this << std::endl; } ~Int() { std::cout << "Destroy Int with value " << value << " at " << this << std::endl; } voidPrint()const{ std::cout << "Print value: " << value << std::endl; } }; voidfunc(Int it){ it.Print(); } intmain(){ Int a{ 10 }; func(a); }
1 2 3 4 5
Create Int with value 10 at 00CFF8C8 Copy create Int with value 10 at 00CFF7E4 Print value: 10 Destroy Int with value 10 at 00CFF7E4 Destroy Int with value 10 at 00CFF8C8
classInt { private: int value; public: Int() :value(0) { std::cout << "Default create Int" << "at" << this << std::endl; } Int(int v) :value(v) { std::cout << "Create Int with value " << value << " at " << this << std::endl; } Int(Int& it) :value(it.value) { std::cout << "Copy create Int with value " << value << " at " << this << std::endl; } ~Int() { std::cout << "Destroy Int with value " << value << " at " << this << std::endl; }
voidPrint()const{ std::cout << "Print value: " << value << std::endl; } }; Int func(Int it){ it.Print(); return it; } intmain(){ Int a{ 10 }; func(a); Int c = 0; c = func(a); }
Create Int with value 10 at 010FF8AC Copy create Int with value 10 at 010FF790 Print value: 10 Copy create Int with value 10 at 010FF7C8 Destroy Int with value 10 at 010FF790 Destroy Int with value 10 at 010FF7C8 Create Int with value 0 at 010FF8A0 Copy create Int with value 10 at 010FF790 Print value: 10 Copy create Int with value 10 at 010FF7B0 Destroy Int with value 10 at 010FF790 Destroy Int with value 10 at 010FF7B0 Destroy Int with value 10 at 010FF8A0 Destroy Int with value 10 at 010FF8AC
classInt { private: int value; public: Int() :value(0) { std::cout << "Default create Int" << "at" << this << std::endl; } Int(int v) :value(v) { std::cout << "Create Int with value " << value << " at " << this << std::endl; } Int(Int& it) :value(it.value) { std::cout << "Copy create Int with value " << value << " at " << this << std::endl; } ~Int() { std::cout << "Destroy Int with value " << value << " at " << this << std::endl; } Int& operator=(const Int& it) { if (this != &it) { value = it.value; } std::cout << this << " operator= " << &it << std::endl; //将亡值赋值给c return *this; } voidPrint()const{ std::cout << "Print value: " << value << std::endl; } };
1 2 3 4 5 6 7 8 9 10
Create Int with value 10 at 00EFFD88 Create Int with value 0 at 00EFFD7C Copy create Int with value 10 at 00EFFC84 Print value: 10 Copy create Int with value 10 at 00EFFCA4 Destroy Int with value 10 at 00EFFC84 00EFFD7C operator= 00EFFCA4 Destroy Int with value 10 at 00EFFCA4 Destroy Int with value 10 at 00EFFD7C Destroy Int with value 10 at 00EFFD88
在返回对象时,不可使用引用:引用的本质为指针,以引用返回会导致 value 从已经释放掉的值获取,这将导致其为随机值.当函数结束但变量仍然存在,则可以使用引用的形式返回
1 2 3 4
Int& func(int x){ static Int tmp; return tmp; }
4. 用对象数组初始化时
当使用对象数组初始化另一个对象数组时,每个元素的初始化都会调用拷贝构造函数:
1 2
Int arr1[2] = {Int(10), Int(20)}; Int arr2[2] = arr1; // 每个元素调用拷贝构造