对象的赋值运算符

时间:2013-05-07 12:24:26

标签: c++ class operator-overloading assignment-operator

我编写了一个代码,用于动态分配名称。我知道我应该在这种情况下处理深层复制。我写的是我自己的Copy Constructor,Copy Assignment Operator和析构函数。我应该重新定义任何其他隐式函数,例如移动赋值运算符。我不清楚Move Assignment Operator的概念或任何其他隐式定义的成员函数(除了我已经提到的)。 任何人都可以添加此dynName code的代码,以显示移动分配运算符或任何其他隐式成员函数(如果有)。

#include <iostream>

using namespace std;

class dynName{
    char* name;
    int size;
    public:

    dynName(char* name="")
    {
        int n=strlen(name)+1;
        this->name= new char[n];
        strncpy(this->name,name,n);
        size=n;
        name[size-1]='\0';//NULL terminated
        cout << "Object created (Constructor) with name : "
        << name << " at address " << &(this->name) << endl;
        }

    dynName(const dynName& Ob)//Copy Constructor
    {
        int n=Ob.size;
        this->name= new char[n];
        strncpy(this->name,Ob.name,n);
        size=n;
        cout << "Object created(Copy constructor) with name : "
        << this->name  << " at address " << &(this->name) << endl;
        }

    //Assignment Operator
    dynName& operator=(const dynName& ob);

    ~dynName()
    {
        cout << "Object with name " << this->name << " at address " <<
        &(this->name)<<" destroyed" << endl;
        delete[] name;
        name=0; //Avoiding Dangling pointer if any
        }
    //friend ostream& operator << (ostream& os,const dynName ob);
    //Will Call Copy Constructor

    friend ostream& operator << (ostream& os,const dynName& ob);
    };

dynName& dynName::operator=(const dynName& ob)
{
    // check for self-assignment
        if (this == &ob)
        cout << "Created with assignment Operator " << endl;
            return *this;

        // first we need to deallocate any value that this string is holding!
        delete[] this->name;


        this->size = ob.size;

           // this->name = new char[this->size];
            strncpy(this->name, ob.name,this->size);
            cout << "Created with assignment Operator " << endl;

    return *this;
    }

//ostream& operator << (ostream& os,const dynName ob)
ostream& operator << (ostream& os,const dynName& ob)
{
    os << "The name ("<< ob.size << " Letters) : " << ob.name << endl;
    return os;
    }

int main()
{

    dynName Ob1("Andrew Thomas");
    dynName Ob2;
    dynName Ob3(Ob1);
    dynName Ob4;
    Ob4=Ob1;//Should Call Assignment Operator
    cout << "\n\n\n";
    cout << Ob1;
    cout << Ob2;
    cout << Ob3;
    cout << Ob4;
    cout << "\n\n\n";

    return 0;
    }

此代码的问题在于没有调用我的复制赋值运算符。任何帮助,为什么会这样?

$ ./Trial

Object created (Constructor) with name : Andrew Thomas at address 0x22ac40
Object created (Constructor) with name :  at address 0x22ac30
Object created(Copy constructor) with name : Andrew Thomas at address 0x22ac20
Object created (Constructor) with name :  at address 0x22ac10



The name (14 Letters) : Andrew Thomas
The name (1 Letters) :
The name (14 Letters) : Andrew Thomas
The name (1 Letters) :



Object with name  at address 0x22ac10 destroyed
Object with name Andrew Thomas at address 0x22ac20 destroyed
Object with name  at address 0x22ac30 destroyed
Object with name Andrew Thomas at address 0x22ac40 destroyed

由于

修改

参考Move assignment operator and `if (this != &rhs)` 什么是Class&&?我的意思是我从来没有使用过这类东西..只是引用,即Class&

2 个答案:

答案 0 :(得分:5)

看来你在这里缺少大括号:

   if (this == &ob)
    cout << "Created with assignment Operator " << endl;
        return *this;

只有输出是if主体的一部分,return语句将始终执行。

答案 1 :(得分:4)

应该调用复制操作符,但是在自我分配检查后总是返回。

dynName& dynName::operator=(const dynName& ob)
{
    // check for self-assignment
        if (this == &ob)
        cout << "Created with assignment Operator " << endl;
            return *this;  //THIS LINE is not in the if block

        // first we need to deallocate any value that this string is holding!
        delete[] this->name;


        this->size = ob.size;

           // this->name = new char[this->size];
            strncpy(this->name, ob.name,this->size);
            cout << "Created with assignment Operator " << endl;

    return *this;
    }