• 安全性良好的operator=操作,和新的new方法(针对深度复制的情况)


    class B
    {
    };
    class A
    {
    public:
         A& operator=(const A& a)
        {
            B* temp = b; //这里解决重复赋值的方法是用temp指向自己,如果一样,下面的    delete会删掉
            b = new B(*a.b);//关键复制,就是把b=new B;b=a.b;分开写
            delete temp; //删掉过去的自己,delete 尽量后面写,万一delete后有抛出异常这些什么就很尴尬
       return *this; //上面那句delete一定是new的
    }
    B* b;
    
    };
    
    int main()
    {
    	int a = 3;
    	int *b = &a;
    	int *c = new int(*b);//新的new 方法,两条语句合在一起写
    	int *d = new int;//和上面的同一个意思
    	d = b;
    	int *e = new int(a);//和上面的一个意思
         int *f = new int(3);//和上面的一个意思 cout << *b << endl << *c << endl << *d << endl << *e; }

      

      还有一种copy and swap的方法

    private:
    	int a;
    	void swapp(A& x)//在自己特有的swap上调用标准的swap
    	{
    		swap(x.a, a);
    		//swap(其他数据)
    	}
    public:
    	A(int x) :a(x){}
    	A& operator=( A x)
    	{
    		swapp(x);
    		return *this;
    	}
    

      这个是我用过的旧版本

    class A
    {
    	int a;
    public:
    	A(int x) :a(x){}
    	template<typename T >//使用自己特例化的swap
    	void swap(T& a)
    	{
    		int itemp = this->a;
    		this->a = a.a;
    		a.a = itemp;
    	}
    	A& operator=(A x)//和下面的一样,少了传值复制这个步骤,代码更加整洁,建议用这个
    	{
    		swap(x);//原来有的数据会被当作临时数据而delete掉
    		return *this;
    	}
    	A& operator=(const A& x)
    	{
    		A temp = x;
    		swap( temp);
    		return *this;
    	}
    	void display()
    	{
    		cout << a << endl;
    	}
    };
    int main()
    {
    	A a(3), b(1);
    	A c(a);
    	b = a;//测试赋值
    	a = a;//测试自我赋值
    	a.display();
    	b.display();
    	c.display();
    }
    

      以前没有特例化swap函数而导致无限重复调用的失败例子

    swap(temp, *this);
    我貌似知道为什么不行了
    swap里面有temp=*this之类的语句
    就会调用operator=的函数然后无限重复
    所以要用自己特例化的swap

    void swapp(T a, T b)        //和下面重复调用
    {
    	T temp = a;
    	a = b;
    	b = temp;
    }
    
    class A
    {
    	int a;
    public:
    	A(int x) :a(x){}
    	A& operator=( A x)   //和上面重复调用
    	{
    		swapp(*this,x);
    		return *this;
    	}
    

      

      

    
    
  • 相关阅读:
    设计模式之依赖倒置原则
    设计模式之里氏替换原则
    设计模式之单一职责原则
    设计模式六大原则
    spring boot 搭建
    OSI(Open System Interconnect) 网络七层协议
    常用经济术语
    Spring Boot入门系列(十八)整合mybatis,使用注解的方式实现增删改查
    Spring Boot入门系列(十四)使用JdbcTemplate操作数据库,配置多数据源!
    Maven快速入门(三)Maven的坐标和仓库
  • 原文地址:https://www.cnblogs.com/vhyc/p/5581555.html
Copyright © 2020-2023  润新知