简体   繁体   中英

Assignment Operator for an object

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 ie 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 should be calling the copy operator, but you always return after the self-assignment check.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM