c++ - Difference between Copy Constructor and Assigment Operator -
this question has answer here:
i'm having little trouble copy constructor.
i have class, contains 2 structures, , 2 pointers first structure ( let's i'm having linked list of first structure, , each contains linked list of second structure ). seem work fine. ...
when make instance of class using copy constructor ( deep copy, every element copied, each instance has it's own linked lists ) using
myclass a,b; // operations b ( );
it works ok. then...
myclass a,b; // operations b = a;
also seems work, destructor goes amok, , tries free element multiple times, sending this:
* error in `./a.out': double free or corruption (!prev): 0x000000000258a540 *
along ==backtrace== , ==memory map==, ending killing program sigabrt signal.
so, when copy constructor works fine , present, wrong assignment? should override operator= ?
thanks
if not have defined assignment operator explicitly, obvious code broken. implicit assignment operator member-wise assignment of each data member assigned object. if have member variables e.g. live on heap standard implicit assignment operator not deep copy , copies pointer values.
Comments
Post a Comment