用拷贝构造函数

为啥要拷贝对象:
比如 :一个数组中装了一些对象,但是数组内存不够了。要重新申请一块内存装数据
c 语言中会直接拷贝一块内存
以下是c++ 的做法
第一种 CObject y(x);
#include <stdlib.h>#include <Windows.h>#include <stdio.h>class CObject {private:int x;int y;public:CObject() { // 无参构造函数}CObject(int x, int y) { // 有参构造函数this->x = x;this->y = y;}};int main() {CObject obj(1, 2);CObject objNew(obj);//拷贝构造函数return 0;}

第二种
CObject * p=new CObject(x);
int main() {CObject obj(1, 2);//CObject objNew(obj);//拷贝构造函数CObject* p = new CObject(obj);return 0;}
CObject obj(1, 2);00007FF62923197B mov r8d,200007FF629231981 mov edx,100007FF629231986 lea rcx,[obj]00007FF62923198A call CObject::CObject (07FF62923137Fh)//CObject objNew(obj);//拷贝构造函数CObject* p = new CObject(obj);00007FF62923198F mov ecx,800007FF629231994 call operator new (07FF62923103Ch)00007FF629231999 mov qword ptr [rbp+108h],rax00007FF6292319A0 cmp qword ptr [rbp+108h],000007FF6292319A8 je main+88h (07FF6292319C8h)00007FF6292319AA mov rax,qword ptr [rbp+108h]00007FF6292319B1 mov rcx,qword ptr [obj]00007FF6292319B5 mov qword ptr [rax],rcx00007FF6292319B8 mov rax,qword ptr [rbp+108h]00007FF6292319BF mov qword ptr [rbp+118h],rax00007FF6292319C6 jmp main+93h (07FF6292319D3h)00007FF6292319C8 mov qword ptr [rbp+118h],000007FF6292319D3 mov rax,qword ptr [rbp+118h]00007FF6292319DA mov qword ptr [p],raxreturn 0;
CObject(int x, int y) { // 有参构造函数00007FF6292318A0 mov dword ptr [rsp+18h],r8d00007FF6292318A5 mov dword ptr [rsp+10h],edx00007FF6292318A9 mov qword ptr [rsp+8],rcx00007FF6292318AE push rbp00007FF6292318AF push rdi00007FF6292318B0 sub rsp,0E8h00007FF6292318B7 lea rbp,[rsp+20h]00007FF6292318BC mov rdi,rsp00007FF6292318BF mov ecx,3Ah00007FF6292318C4 mov eax,0CCCCCCCCh00007FF6292318C9 rep stos dword ptr [rdi]00007FF6292318CB mov rcx,qword ptr [rsp+108h]00007FF6292318D3 lea rcx,[__7A1276C5_源@cpp (07FF629243015h)]00007FF6292318DA call __CheckForDebuggerJustMyCode (07FF6292313D4h)this->x = x;00007FF6292318DF mov rax,qword ptr [this]00007FF6292318E6 mov ecx,dword ptr [x]00007FF6292318EC mov dword ptr [rax],ecxthis->y = y;00007FF6292318EE mov rax,qword ptr [this]00007FF6292318F5 mov ecx,dword ptr [y]00007FF6292318FB mov dword ptr [rax+4],ecx}00007FF6292318FE mov rax,qword ptr [this]00007FF629231905 lea rsp,[rbp+0C8h]00007FF62923190C pop rdi00007FF62923190D pop rbp00007FF62923190E ret
_CRT_SECURITYCRITICAL_ATTRIBUTEvoid* __CRTDECL operator new(size_t const size){00007FF629231A40 mov qword ptr [rsp+8],rcx00007FF629231A45 sub rsp,38hfor (;;){if (void* const block = malloc(size))00007FF629231A49 mov rcx,qword ptr [size]00007FF629231A4E call malloc (07FF62923123Ah)00007FF629231A53 mov qword ptr [rsp+20h],rax00007FF629231A58 cmp qword ptr [rsp+20h],000007FF629231A5E je operator new+27h (07FF629231A67h){return block;00007FF629231A60 mov rax,qword ptr [rsp+20h]00007FF629231A65 jmp operator new+4Bh (07FF629231A8Bh)}if (_callnewh(size) == 0)00007FF629231A67 mov rcx,qword ptr [size]00007FF629231A6C call _callnewh (07FF6292312EEh)00007FF629231A71 test eax,eax00007FF629231A73 jne operator new+49h (07FF629231A89h){if (size == SIZE_MAX)00007FF629231A75 cmp qword ptr [size],0FFFFFFFFFFFFFFFFh00007FF629231A7B jne operator new+44h (07FF629231A84h){__scrt_throw_std_bad_array_new_length();00007FF629231A7D call __scrt_throw_std_bad_array_new_length (07FF629231410h)}00007FF629231A82 jmp operator new+49h (07FF629231A89h)else{__scrt_throw_std_bad_alloc();00007FF629231A84 call __scrt_throw_std_bad_alloc (07FF629231118h)}}// The new handler was successful; try to allocate again...}00007FF629231A89 jmp operator new+9h (07FF629231A49h)}00007FF629231A8B add rsp,38h00007FF629231A8F ret
重要
对象浅拷贝 不会拷贝指针指向内容
对象拷贝存在一个巨大的问题
一旦对象中存在指针,会连指针一起拷贝 ,如果另一个对象指针指向内容被释放 ;另一个对象再使用 就会产生指针悬空的危险
自己重写的深拷贝 构造函数,增加指针内容拷贝
重载赋值运算符
#include <stdlib.h>#include <Windows.h>#include <stdio.h>class CBase{public:int x, y;CBase(int x,int y) {this->x = x;this->y = y;};private:};int main() {CBase c1(1, 2), c2(3, 4);c1 = c2;return 0;}

CBase c1(1, 2), c2(3, 4);00007FF6CCF7183B mov r8d,200007FF6CCF71841 mov edx,100007FF6CCF71846 lea rcx,[c1]00007FF6CCF7184A call CBase::CBase (07FF6CCF7115Eh)00007FF6CCF7184F mov r8d,400007FF6CCF71855 mov edx,300007FF6CCF7185A lea rcx,[c2]00007FF6CCF7185E call CBase::CBase (07FF6CCF7115Eh)c1 = c2;00007FF6CCF71863 mov rax,qword ptr [c2]00007FF6CCF71867 mov qword ptr [c1],raxreturn 0;00007FF6CCF7186B xor eax,eax}
父类构造函数
= 是可以处理父类数据的
不过和拷贝构造函数一样都是浅拷贝
#include <stdlib.h>
#include <Windows.h>
#include <stdio.h>
class CBase
{
public:
int x, y;
CBase(int x,int y) {
this->x = x;
this->y = y;
};
private:
};
class CSub:CBase
{
public:
CSub(int x,int y,int z):CBase(x,y) {
this->z = z;
};
private:
int z;
};
int main() {
CSub c1(1,2,3), c2(3,4,5), c3();
c1 = c2;
return 0;
}
