【问题标题】:Assignment Operator for an object对象的赋值运算符
【发布时间】:2013-05-01 09:41:07
【问题描述】:

我编写了一个代码,用于动态分配名称。我知道我应该在这种情况下处理深拷贝。我写的是我自己版本的复制构造函数、复制赋值运算符和析构函数。我是否应该重新定义任何其他隐式函数,例如 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;
    }

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

$ ./试用

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&amp;&amp; 是什么?我的意思是我从来没有使用过这种东西..只是参考,即Class&amp;

【问题讨论】:

  • 你发布的代码对我来说崩溃了。
  • 不要重新发明另一个损坏的字符串类,使用 std::string 字段并让它处理内存管理。
  • 我已从您的标题中删除了“动态”字样,因为它在这里无关紧要。在您的 int main 中,所有 ObX 对象都不是动态的。它们的内部内容是,但对象不是。
  • @quetzalcoatl 为什么它不是动态的? It has dynamically allocated variable which is not fixed in size and created with 'new' ...learncpp.com/cpp-tutorial/912-shallow-vs-deep-copying
  • @jrok 取消注释 // this-&gt;name = new char[this-&gt;size]; 并放置大括号 if (this == &amp;ob){ cout &lt;&lt; "Created with assignment Operator " &lt;&lt; endl; return *this;} 效果很好

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


【解决方案1】:

它应该调用复制操作符,但是你总是在自赋值检查之后返回。

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;
    }

【讨论】:

  • 代码中注入的注释很容易被忽略。您应该在答案的文本中详细说明。
【解决方案2】:

您似乎在这里缺少大括号:

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

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

【讨论】:

    猜你喜欢
    • 2013-10-13
    • 1970-01-01
    • 2011-10-16
    • 1970-01-01
    • 2011-08-02
    • 2019-10-06
    • 2011-11-16
    • 2013-11-30
    • 2015-10-02
    相关资源
    最近更新 更多