This is the exact point I am in in C++ Primer Plus Third Edition.
The overloaded assignment operator is used when you assign one object to another.
ie
string motto("Home of the Griz");
string ditto;
ditto=motto; // uses overloaded assignment operator
When initalizing an object the copy constructor is used ie
string motto=ditto;
The implict implementation of the assignment operator performs a member-to-member copy. If the member is itself an object of some class, the program uses the assignment operator defined for the class to do the copying for that paticular member. Static members are unaffected.
member-by-member copying copies the values of pointers instead of the pointed to data. When the destructor is called for ditto, it deletes the string "Home of the Griz" and when in calls the destructor for motto it attempts to delete the previously deleted string.
classes that use the new operator to allocate memory pointed to by a class member
You should define a member function overloading the assignment operator
Code:
c_name & cname::operator=(const c_name & cn)
{
if (this == & cn_)
return *this; // done if self assignment
delete c_pointer;
c_pointer = new type_name[size];
// then copy the data pointed to by cn.c_pointer to
// location pointed to by c_pointer
.....
return *this;
}