用拷贝构造函数

image.png
为啥要拷贝对象:
比如 :一个数组中装了一些对象,但是数组内存不够了。要重新申请一块内存装数据
c 语言中会直接拷贝一块内存

以下是c++ 的做法
第一种 CObject y(x);

  1. #include <stdlib.h>
  2. #include <Windows.h>
  3. #include <stdio.h>
  4. class CObject {
  5. private:
  6. int x;
  7. int y;
  8. public:
  9. CObject() { // 无参构造函数
  10. }
  11. CObject(int x, int y) { // 有参构造函数
  12. this->x = x;
  13. this->y = y;
  14. }
  15. };
  16. int main() {
  17. CObject obj(1, 2);
  18. CObject objNew(obj);//拷贝构造函数
  19. return 0;
  20. }

image.png
第二种
CObject * p=new CObject(x);

  1. int main() {
  2. CObject obj(1, 2);
  3. //CObject objNew(obj);//拷贝构造函数
  4. CObject* p = new CObject(obj);
  5. return 0;
  6. }
  1. CObject obj(1, 2);
  2. 00007FF62923197B mov r8d,2
  3. 00007FF629231981 mov edx,1
  4. 00007FF629231986 lea rcx,[obj]
  5. 00007FF62923198A call CObject::CObject (07FF62923137Fh)
  6. //CObject objNew(obj);//拷贝构造函数
  7. CObject* p = new CObject(obj);
  8. 00007FF62923198F mov ecx,8
  9. 00007FF629231994 call operator new (07FF62923103Ch)
  10. 00007FF629231999 mov qword ptr [rbp+108h],rax
  11. 00007FF6292319A0 cmp qword ptr [rbp+108h],0
  12. 00007FF6292319A8 je main+88h (07FF6292319C8h)
  13. 00007FF6292319AA mov rax,qword ptr [rbp+108h]
  14. 00007FF6292319B1 mov rcx,qword ptr [obj]
  15. 00007FF6292319B5 mov qword ptr [rax],rcx
  16. 00007FF6292319B8 mov rax,qword ptr [rbp+108h]
  17. 00007FF6292319BF mov qword ptr [rbp+118h],rax
  18. 00007FF6292319C6 jmp main+93h (07FF6292319D3h)
  19. 00007FF6292319C8 mov qword ptr [rbp+118h],0
  20. 00007FF6292319D3 mov rax,qword ptr [rbp+118h]
  21. 00007FF6292319DA mov qword ptr [p],rax
  22. return 0;
  1. CObject(int x, int y) { // 有参构造函数
  2. 00007FF6292318A0 mov dword ptr [rsp+18h],r8d
  3. 00007FF6292318A5 mov dword ptr [rsp+10h],edx
  4. 00007FF6292318A9 mov qword ptr [rsp+8],rcx
  5. 00007FF6292318AE push rbp
  6. 00007FF6292318AF push rdi
  7. 00007FF6292318B0 sub rsp,0E8h
  8. 00007FF6292318B7 lea rbp,[rsp+20h]
  9. 00007FF6292318BC mov rdi,rsp
  10. 00007FF6292318BF mov ecx,3Ah
  11. 00007FF6292318C4 mov eax,0CCCCCCCCh
  12. 00007FF6292318C9 rep stos dword ptr [rdi]
  13. 00007FF6292318CB mov rcx,qword ptr [rsp+108h]
  14. 00007FF6292318D3 lea rcx,[__7A1276C5_源@cpp (07FF629243015h)]
  15. 00007FF6292318DA call __CheckForDebuggerJustMyCode (07FF6292313D4h)
  16. this->x = x;
  17. 00007FF6292318DF mov rax,qword ptr [this]
  18. 00007FF6292318E6 mov ecx,dword ptr [x]
  19. 00007FF6292318EC mov dword ptr [rax],ecx
  20. this->y = y;
  21. 00007FF6292318EE mov rax,qword ptr [this]
  22. 00007FF6292318F5 mov ecx,dword ptr [y]
  23. 00007FF6292318FB mov dword ptr [rax+4],ecx
  24. }
  25. 00007FF6292318FE mov rax,qword ptr [this]
  26. 00007FF629231905 lea rsp,[rbp+0C8h]
  27. 00007FF62923190C pop rdi
  28. 00007FF62923190D pop rbp
  29. 00007FF62923190E ret
  1. _CRT_SECURITYCRITICAL_ATTRIBUTE
  2. void* __CRTDECL operator new(size_t const size)
  3. {
  4. 00007FF629231A40 mov qword ptr [rsp+8],rcx
  5. 00007FF629231A45 sub rsp,38h
  6. for (;;)
  7. {
  8. if (void* const block = malloc(size))
  9. 00007FF629231A49 mov rcx,qword ptr [size]
  10. 00007FF629231A4E call malloc (07FF62923123Ah)
  11. 00007FF629231A53 mov qword ptr [rsp+20h],rax
  12. 00007FF629231A58 cmp qword ptr [rsp+20h],0
  13. 00007FF629231A5E je operator new+27h (07FF629231A67h)
  14. {
  15. return block;
  16. 00007FF629231A60 mov rax,qword ptr [rsp+20h]
  17. 00007FF629231A65 jmp operator new+4Bh (07FF629231A8Bh)
  18. }
  19. if (_callnewh(size) == 0)
  20. 00007FF629231A67 mov rcx,qword ptr [size]
  21. 00007FF629231A6C call _callnewh (07FF6292312EEh)
  22. 00007FF629231A71 test eax,eax
  23. 00007FF629231A73 jne operator new+49h (07FF629231A89h)
  24. {
  25. if (size == SIZE_MAX)
  26. 00007FF629231A75 cmp qword ptr [size],0FFFFFFFFFFFFFFFFh
  27. 00007FF629231A7B jne operator new+44h (07FF629231A84h)
  28. {
  29. __scrt_throw_std_bad_array_new_length();
  30. 00007FF629231A7D call __scrt_throw_std_bad_array_new_length (07FF629231410h)
  31. }
  32. 00007FF629231A82 jmp operator new+49h (07FF629231A89h)
  33. else
  34. {
  35. __scrt_throw_std_bad_alloc();
  36. 00007FF629231A84 call __scrt_throw_std_bad_alloc (07FF629231118h)
  37. }
  38. }
  39. // The new handler was successful; try to allocate again...
  40. }
  41. 00007FF629231A89 jmp operator new+9h (07FF629231A49h)
  42. }
  43. 00007FF629231A8B add rsp,38h
  44. 00007FF629231A8F ret

重要

对象浅拷贝 不会拷贝指针指向内容
对象拷贝存在一个巨大的问题
一旦对象中存在指针,会连指针一起拷贝 ,如果另一个对象指针指向内容被释放 ;另一个对象再使用 就会产生指针悬空的危险
自己重写的深拷贝 构造函数,增加指针内容拷贝
image.png

image.png

重载赋值运算符

  1. #include <stdlib.h>
  2. #include <Windows.h>
  3. #include <stdio.h>
  4. class CBase
  5. {
  6. public:
  7. int x, y;
  8. CBase(int x,int y) {
  9. this->x = x;
  10. this->y = y;
  11. };
  12. private:
  13. };
  14. int main() {
  15. CBase c1(1, 2), c2(3, 4);
  16. c1 = c2;
  17. return 0;
  18. }

image.png

  1. CBase c1(1, 2), c2(3, 4);
  2. 00007FF6CCF7183B mov r8d,2
  3. 00007FF6CCF71841 mov edx,1
  4. 00007FF6CCF71846 lea rcx,[c1]
  5. 00007FF6CCF7184A call CBase::CBase (07FF6CCF7115Eh)
  6. 00007FF6CCF7184F mov r8d,4
  7. 00007FF6CCF71855 mov edx,3
  8. 00007FF6CCF7185A lea rcx,[c2]
  9. 00007FF6CCF7185E call CBase::CBase (07FF6CCF7115Eh)
  10. c1 = c2;
  11. 00007FF6CCF71863 mov rax,qword ptr [c2]
  12. 00007FF6CCF71867 mov qword ptr [c1],rax
  13. return 0;
  14. 00007FF6CCF7186B xor eax,eax
  15. }

父类构造函数

= 是可以处理父类数据的
不过和拷贝构造函数一样都是浅拷贝

#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;
}

深度拷贝

https://www.bilibili.com/video/BV1zU4y1a7ZA?p=46