Why and when should use a Copy Constructor in C++?

Lets say we create an object of class Student, we create two instances/objects class Student(i.e. StudentA and StudentB). We do shallow copy to initialize data members of B with that of A as follows:

Student StudentB = StudentA;

And then we destroy StudentB. Are we facing a situation of Dangling Pointer here? Why and how? Please explain.

✅ Answers

? Favorite Answer

  • It would happen if one of the data members of class Student was an std::auto_ptr, because copying of auto pointers (unlike shared pointers) transfers ownership from StudentA to StudentB, and destruction of StudentB deletes the pointed-to object, leaving StudentA with a pointer to non-existing object.

    Likewise if one of the members was a raw pointer and if the destructor of Student calls delete on it, but who uses raw pointers in C++ anymore? It’s like using goto in C.

    Chris C: he said “shallow copy”

  • Whenever code readability is a requirement. Which should be always.

    There won’t be a dangling pointer, as long as the local variables that are defined in the class are an actual copy instead of a copy of the pointer.

    An example of this would be:

    class MyClass

    {

    public:

       MyClass()

       {

          aString = calloc(, sizeof(char));

       };

       ~MyClass()

       {

          if (aString)

             free(aString);

       };

       

       operator =(const MyClass &rightSide)

       {

          if (this->aString == NULL)

          {

             this->aString = calloc(, sizeof(char));

             strcpy(this->aString, rightSide->aString);

          }

       };

       /*

          Do NOT do this:

       operator =(const MyClass &rightSide)

       {

          this->aString = rightSide->aString;

       };

       */

    protected:

       char *aString;

    };

  • Other Related Questions

    Learning Visual Basic ?

    Answers Favorite AnswerTry using "System.Diagnostics.Process. GetProcessesByName( "iwmp" ).Length > " for your check.

    See also  How to make a hover over blog box?
    Microsoft Office word Fast answer needed its urgent?

    Answers Favorite AnswerTry this:http://www.techsupport.com///microsoft-wo...Here is another thread re: the same issue with instructions:http://www.pcreview.co.uk/forums/modification-not-...http://www.techsupport.com///microsoft-wo...http://support.microsoft.com/kb/http://answers.microsoft.com/en-us/office/forum/of...

    Can I download a whole city android google maps?

    Answers Favorite Answer:) Yes!!!Interesting question. I wonder why it has blocked in this way especially considering that Google Earth is very detailed and has good maps of Israel.

    What is meant by ROM ? Explain in simple but elaborate terms.What about mobile ROM’s?

    Answers Favorite AnswerHi Diva below is a link that will give a simple answer.http://wiki.answers.com/Q/What_does_ROM_stand_for_...Hope this helps.Source(s): Experience and wiki answers.ROM is Read only memory. i.e data can write only once.There two types of ROM..ROM.PROMThe difference between ROM and PROM.that is ROM is programmed during manufacturing it means data stored by manufacturing company.PROM is blank memory that a user programmable memory.user can store content on PROM.both ROM and PROM are Read only memory Data can write only once.and its not possible to write so many time.Memories of PROM and ROM are Non-volatile in nature. Its that stored informations can retain even power goes off.

    Leave a Comment