分配对象的操作符 [英] Assignment Operator for an object

查看:142
本文介绍了分配对象的操作符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  #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:
<<名称<< at address<< &(this-> name)<< endl;
}

dynName(const dynName& Ob)//复制构造函数
{
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;
}

//赋值运算符
dynName& operator =(const dynName& ob);

〜dynName()
{
cout< 具有名称的对象< this-> name<< at address<<
&(this-> name)<<destroyed< endl;
delete [] name;
name = 0; //避免Dangling指针(如果有的话)
}
// friend ostream&运算符<< (ostream& os,const dynName ob);
//将调用复制构造函数

friend ostream&运算符<< (ostream& os,const dynName& ob);
};

dynName& dynName :: operator =(const dynName& ob)
{
//检查自我分配
if(this ==& ob)
cout< Created with assignment Operator< endl;
return * this;

//首先我们需要释放这个字符串持有的任何值!
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&运算符<< (ostream& os,const dynName ob)
ostream&运算符<< (ostream& os,const dynName& ob)
{
os< 名称(<< obsize<<Letters):< ob.name<< endl;
return os;
}

int main()
{

dynName Ob1(Andrew Thomas);
dynName Ob2;
dynName Ob3(Ob1);
dynName Ob4;
Ob4 = Ob1; //应该调用赋值运算符
cout<< \\\
\\\
\\\
;
cout<< Ob1;
cout<< Ob2;
cout<< Ob3;
cout<< Ob4;
cout<< \\\
\\\
\\\
;

return 0;
}

此代码的问题是

 

$

对象创建(构造函数)名称:Andrew Thomas在地址0x22ac40
对象创建(构造函数)与名称:在地址0x22ac30
创建的对象(复制构造函数)地址0x22ac20
创建的对象(构造函数)名称:在地址0x22ac10



名称(14个字母):Andrew Thomas
名称字母):
名称(14个字母):Andrew Thomas
名称(1个字母):



对象名称位于地址0x22ac10
名为Andrew Thomas的对象在地址0x22ac20已销毁
对象名称在地址0x22ac30销毁
对象名为Andrew Thomas在地址0x22ac40销毁

感谢



编辑



参考移动赋值运算符和`if(this!=& ; rhs)`
什么是类&& ?我的意思是我从来没有使用过这样的东西。只是引用ie Class&

解决方案

这里似乎缺少大括号:

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

只有输出是如果 body, return 语句将始终被执行。


I have written a code, for dynamically allocating a name. I know I should take care of deep copy in such scenarios. What I have written is my own version of Copy Constructor,Copy Assignment Operator and destructor. Should I redefine any other implicit functions such as Move Assignment Operator . I am not clear with the concept of Move Assignment Operator or any other implicitly defined member functions (other than which I have already mentioned ). Can any one please add the code for this dynName code ,to show Move assignment operator or any other implicit member function (if any).

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

The problem with this code is that it is not calling my copy assignment operator.Any help , why so ?

$ ./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

Thanks

EDIT

Referring to Move assignment operator and `if (this != &rhs)` What is Class&& ? I mean I have never used something of this sort .. Just references i.e Class&

解决方案

It seems you are missing braces around here:

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

Only the output is part of the if body, the return statement will always be executed.

这篇关于分配对象的操作符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆