C语言 c++ php mysql nginx linux lnmp lamp lanmp memcache redis 面试 笔记 ppt 设计模式 问题 远程连接

c++学习-运算符重载

 

重载=号运算符,由于成员属性中有指针会出现错误

#include <iostream>
using namespace std;

class num{
public:
    num(){n=new int;*n=1;cout<<"construct:"<<endl;}

    num(int x){n=new int;*n=x;cout<<"construct:"<<endl;}

    ~num(){delete n;n=NULL; cout<<"destruct:"<<endl;}

    //num(num &a){this->x=a.x;cout<<"copy:"<<x<<endl;}

    int getX(){
        return *n;
    }
    
    void setX(int x)
    {
        *n=x;
    }

    num operator=(num &r){
        cout<<"operator+"<<endl;
        *n=r.getX();
        return *this; //返回two的副本,two的副本返回后进行析构,导致n指向的内存释放
    }

private:
    int *n;

};

int & test(int & x)
{
    cout<<x<<endl;
    return x;
}

int main()
{
    
    num one,two,three;
    one.setX(110);
    two=one; //<==> two.operator =(one);
    

    cout<<two.getX()<<endl;
    

    return 0;

}

 

解决上面的错误:(深拷贝)

#include <iostream>
using namespace std;

class num{
public:
    num(){n=new int;*n=1;cout<<"construct:"<<endl;}

    num(int x){n=new int;*n=x;cout<<"construct:"<<endl;}

    ~num(){delete n;n=NULL; cout<<"destruct:"<<endl;}

    num(const num & a){n=new int;*n=a.getX();cout<<"copy:"<<endl;}

    int getX() const {
        return *n;
    }
    
    void setX(int x)
    {
        *n=x;
    }

     num operator=(num &r){
        cout<<"operator+"<<endl;
        *n=r.getX();
        return *this; //返回two的副本,two的副本返回后进行析构,导致n指向的内存释放
    }

private:
    int *n;

};


int main()
{
    
    num one,two,three;
    one.setX(110);
    three=two=one; //<==> two.operator =(one);
    

    cout<<one.getX()<<endl;
    cout<<two.getX()<<endl;
    cout<<three.getX()<<endl;
    

    return 0;

}

 

解决上面的错误(引用方式返回)、

#include <iostream>
using namespace std;

class num{
public:
    num(){n=new int;*n=1;cout<<"construct:"<<endl;}

    num(int x){n=new int;*n=x;cout<<"construct:"<<endl;}

    ~num(){delete n;n=NULL; cout<<"destruct:"<<endl;}

    num(const num & a){n=new int;*n=a.getX();cout<<"copy:"<<endl;}

    int getX() const {
        return *n;
    }
    
    void setX(int x)
    {
        *n=x;
    }

     const num & operator=(const num &r){
        cout<<"operator+"<<endl;

        if(this == &r)
        {
            return *this;
        }

        *n=r.getX();
        return *this; //返回two的副本,two的副本返回后进行析构,导致n指向的内存释放
    }

private:
    int *n;

};

class man{
public :
    man(int x){a=x;}
    man(){}
public :
    int a;
};

int main()
{
    num one(100),two,three;
    three=two=one;

    cout<<one.getX()<<endl;
    cout<<two.getX()<<endl;
    cout<<three.getX()<<endl;

    return 0;

}

 

#include <iostream>
using namespace std;

class num{
public:
    num(){n=new int;*n=1;cout<<"construct:"<<endl;}

    num(int x){n=new int;*n=x;cout<<"construct:"<<endl;}

    ~num(){delete n;n=NULL; cout<<"destruct:"<<endl;}

    num(const num & a){n=new int;*n=a.getX();cout<<"copy:"<<endl;}

    int getX() const {
        return *n;
    }
    
    void setX(int x)
    {
        *n=x;
    }

    // const num & operator=(const num &r){
    //    cout<<"operator+"<<endl;

    //    if(this == &r)
    //    {
    //        return *this;
    //    }

    //    *n=r.getX();
    //    return *this; //返回two的副本,two的副本返回后进行析构,导致n指向的内存释放
    //}

private:
    int *n;

};

class man{
public :
    man(int x){a=x;}
    man(){}
public :
    int a;
};

int main()
{
    num one(100),two,three;
    two=one;

/**
错误原因:当执行 two=one; 后,两个对象的成员属性,执行了同一内存地址,其中一个先析构了
遍释放了a执行的内存地址,另个对象在析构时便会报错了

*/

    cout<<one.getX()<<endl;
    cout<<two.getX()<<endl;

    return 0;

}

 

类型转换

#include <iostream>
using namespace std;

class A{
public:
    A(int x, int y=5){i=x; cout<<"construct"<<i<<endl;}
    ~A(){cout<<"destruct"<<i<<endl;}
    void geti(){cout<<i<<endl;}
private:
    int i;
};

int main()
{
    A a(10);
    a=20; //相当于 a= A(20); ,先创建一个临时的对象,然后将这个临时对象赋给对象a,完成赋值后调用临时对象的析构函数
    
    //或 a=A(20);
    

    return 0;

}

 

posted on 2015-06-20 23:02  思齐_  阅读(274)  评论(0编辑  收藏  举报