1

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&

8
  • The code you posted crashes for me. Commented May 7, 2013 at 12:28
  • 3
    Don't reinvent yet another broken string class, use a std::string field and let it deal with memory management. Commented May 7, 2013 at 12:28
  • I've removed 'dynamic' word from your title, as it is irrelevant here. In your int main all ObX objects are not dynamic. Their inner contents are, but the objects are not. Commented May 7, 2013 at 12:29
  • @quetzalcoatl why is it not dynamic ? It has dynamically allocated variable which is not fixed in size and created with 'new' ...learncpp.com/cpp-tutorial/912-shallow-vs-deep-copying Commented May 7, 2013 at 12:42
  • @jrok uncomment // this->name = new char[this->size]; and put braces if (this == &ob){ cout << "Created with assignment Operator " << endl; return *this;} works perfectly fine Commented May 7, 2013 at 12:52

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

Comments

4

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

1 Comment

The comment injected in the code is easily overlooked. You'd have elaborated it in the answer's text instead.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.