zoukankan      html  css  js  c++  java
  • C++拷贝赋值operator=

    拷贝赋值:两个对象之间的拷贝

    默认拷贝赋值导致错误

    #include <iostream>
    class A {
    public:
        A(int i=10){
            m_pi = new int(i);
            ii = i;
            
        }
        A(const A& that) {  
            std::cout << "深拷贝构造函数" << std::endl;
            m_pi = new int;  
            *m_pi = *that.m_pi;    
        }
        void print(void) {
            std::cout << ii << std::endl;
        }
        ~A(void) {
            
            std::cout << "xigouhansu:"<<this << std::endl;
            delete m_pi;
        }
    private:
        int* m_pi;
        int ii;
        
    };
    
    int main()
    {
        A a(100);
        A b;
        b.print();
        //b.operator=(a);
        b = a;//拷贝赋值;与b.operator=(a)等价
        //拷贝赋值:两个对象已经存在
        //编译器其实执行函数:b.operator=(a);
        //如果程序没写这个函数,会执行默认的,在执行析构函数时导致程序中断
        //中断原因:默认的拷贝赋值是直接拷贝各个成员变量值;由于m_pi是指针,a和b指向同一个堆区数据,不能delete两次
        //所以当存在指针类型的成员变量时,要自己写b.operator=(a)函数
        b.print();
        
        
    }

    自写拷贝赋值函数

    #include <iostream>
    class A {
    public:
        A(int i=10){
            m_pi = new int(i);
            ii = i;
            
        }
        A(const A& that) {  
            std::cout << "senkaobei" << std::endl;
            m_pi = new int;  
            *m_pi = *that.m_pi;    
        }
        A& operator=(const A&that) {   //自写拷贝赋值函数
            if (&that != this) {  //防止自赋值
                *m_pi = *that.m_pi;  //拷贝指针的数据
                ii = that.ii;
            }
            return *this;    //返回自身引用
        }
        void print(void) {
            std::cout << ii << std::endl;
            std::cout << *m_pi << std::endl;
        }
        ~A(void) {
            
            std::cout << "xigouhansu:"<<this << std::endl;
            delete m_pi;
        }
    private:
        int* m_pi;
        int ii;
        
    };
    
    int main()
    {
        A a(100);
        A b;
    
        b.print();
        b = a;   //拷贝赋值
         std::cout << &a << std::endl;
        std::cout << &b << std::endl;
        b.print();
        
        
    }

  • 相关阅读:
    uboot的仓库在哪里?
    git如何查看执行过的历史git命令
    for(;;)和while(true)的区别
    CountDownLatch的理解和使用
    countDownLatch
    websocket @ServerEndpoint(value = "/websocket/{ip}")详解
    Java原子性操作之——Atomic包的原理分析
    CAS原理
    java线程池ThreadPoolExecutor的keepAliveTime=0时,表示超过core线程数的线程在空闲时立即结束!!!
    ThreadPoolExecutor 线程池Demo
  • 原文地址:https://www.cnblogs.com/liming19680104/p/14975391.html
Copyright © 2011-2022 走看看