Cpp8 运算符重载和深浅拷贝

admin 2018年5月13日05:16:26评论549 views字数 3040阅读10分8秒阅读模式
摘要

相同类型间可以直接拷贝我们发现使用=直接赋值对象,编译器自动生成了operator= 函数用于处理类型赋值
我们观察编译器自动生成的函数operator=,这里赋值直接略过了虚表(ecx+4)


深浅拷贝

相同类型间可以直接拷贝

// _20180212.cpp : Defines the entry point for the console application. //  #include "stdafx.h"  #include <stdio.h> #include <windows.h>  class A { private:     int* a; public:     A()     {         a = new int[10];     }     virtual ~A()     {         delete a;         printf("析构 A /n");     } };  int main(int argc,char* argv[]) {     A a1;     A a2;     a1 = a2;     return 0; }  //反汇编   27:       A a1; 0040108D   lea         ecx,[ebp-14h] 00401090   call        @ILT+15(BB) (00401014) 004010A4   mov         byte ptr [ebp-4],1 29:       a1 = a2; 004010A8   lea         eax,[ebp-1Ch] 004010AB   push        eax              //a2 作为参数传递 004010AC   lea         ecx,[ebp-14h]    //a1 作为this指针传递 004010AF   call        @ILT+45(Aoperator=: 004012E0   push        ebp 004012E1   mov         ebp,esp 004012E3   sub         esp,44h 004012E6   push        ebx 004012E7   push        esi 004012E8   push        edi 004012E9   push        ecx 004012EA   lea         edi,[ebp-44h] 004012ED   mov         ecx,11h 004012F2   mov         eax,0CCCCCCCCh 004012F7   rep stos    dword ptr [edi] 004012F9   pop         ecx   //eax = a1首地址 004012FA   mov         dword ptr [ebp-4],ecx  004012FD   mov         eax,dword ptr [ebp-4]    //ecx = a2首地址 00401300   mov         ecx,dword ptr [ebp+8] //ecx + 4 这里是 class A中变量a的地址(ecx 对应虚表地址)  //因为ecx是变量a2 所以这里的edx = a2.a    00401303   mov         edx,dword ptr [ecx+4]    //a2.a 赋值给a1.a 00401306   mov         dword ptr [eax+4],edx //返回a1首地址  00401309   mov         eax,dword ptr [ebp-4]  0040130C   pop         edi 0040130D   pop         esi 0040130E   pop         ebx 0040130F   mov         esp,ebp 00401311   pop         ebp 00401312   ret         4 

我们发现使用=直接赋值对象,编译器自动生成了operator= 函数用于处理类型赋值
我们观察编译器自动生成的函数operator=,这里赋值直接略过了虚表(ecx+4)

// _20180212.cpp : Defines the entry point for the console application. //  #include "stdafx.h"  #include <stdio.h> #include <windows.h>  class A { private:     int* a; public:     A()     {         a = new int[10];     }     virtual ~A()     {         delete a;         printf("析构 A /n");     }      int* get_a()     {         return a;     } };  int main(int argc,char* argv[]) {     A a1;     A* a2 = new A;     a1 = *a2;     delete a2;     int* i = a1.get_a();      for (int j =0;j<10;j++)     {         i[j] = j;     }     return 0; }

上述代码看似正常,运行也不报错,原因是虽然delete a2;释放掉了a2的内存,内存管理器虽然认为此内存无用,但是却仍然可以访问,所以这里是在非法使用内存

步骤分析

A a1; A* A2 = new A;

此时a1和a2对应的内存占用如图

Cpp8 运算符重载和深浅拷贝

a1 = *a2;

这一步是无脑拷贝 把a2.a 赋值给 a1.a

此时的内存结构

Cpp8 运算符重载和深浅拷贝

这里 a指向的 int* a 就造成了内存泄漏

delete a2;

这里释放掉a2
内存结构如图

Cpp8 运算符重载和深浅拷贝

这里a2中的int* a成员指向的内存已经释放,
但是此时a1的int* a成员仍然指向a2的成员指向的内存

int* i = a1.get_a();      for (int j =0;j<10;j++)     {         i[j] = j;     }

这里调用get_a 方法 获取 a1.a的地址,把指向的内存赋值。
这里获取到的 int* i指向的已经是个不存在的 非法的内存块
所以这里就造成了内存的非法访问,如果操作系统吧这里的位置分配给其他位置使用
那么就造成了数据混乱,造成比内存泄漏更危险的错误

以上的拷贝方式,就被称为浅拷贝
浅拷贝:只拷贝成员的值
深拷贝:拷贝的不只是成员。还有成员指向的内存的数据

所以在对象拥有指针成员的时候,就需要慎重考虑使用深拷贝还是浅拷贝

运算符重载

一个类,需要比较类的大小

class Number { public:     Number(int x,int y):x(x),y(y)     {     }     bool Max(Number& n)     {         return    this->x>n.x && this->y>n.y;     } private:     int x;     int y; };  int main(int argc, char* argv[]) {     Number n1(1,1),n2(2,2);          bool r = n1.Max(n2);     return 0; }

bool类型,其实就是一个char true是1 false是0

为了比较 n1和n2的大小。我们定义了一个max函数。此时我们写的时候有没有感觉很繁琐,那么我们能不能像基础类型那样使用 >这样的符号来做运算呢 比如

boolr = n1 > n2;

答案是可以的!

// _20180223.cpp : Defines the entry point for the console application. //  #include "stdafx.h"  class Number { public:     Number(int x,int y):x(x),y(y)     {     }     bool operator>(Number& n)     {         return    this->x>n.x && this->y>n.y;     } private:     int x;     int y; };  int main(int argc, char* argv[]) {     Number n1(1,1),n2(2,2);          bool r = n1 > (n2);     return 0; }

重载 ++ -- + - * / > < 等等运算符

class Number { public:     Number(int x,int y):x(x),y(y)     {     }     Number operator++();     Number operator--();     Number operator+(const Number& p);     Number operator-(const Number& p);     Number operator*(const Number& p);     Number operator/(const Number& p);      bool operator<(const Number& p);     bool operator==(const Number& p);     bool operator>(const Number& n)     {         return    this->x>n.x && this->y>n.y;     } private:     int x;     int y; };  int main(int argc, char* argv[]) {     Number n1(1,1),n2(2,2);          bool r = n1 > (n2);     return 0; }

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2018年5月13日05:16:26
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   Cpp8 运算符重载和深浅拷贝https://cn-sec.com/archives/51546.html

发表评论

匿名网友 填写信息