Im writing a copy assignment operator for a class I've created and I'm using a previous post as a guide: What is The Rule of Three?
Im a bit confused on one aspect of this person's explanation.
Here is their class:
class person
{
char* name;
int age;
};
Here is the copy assignment operator definition that I am using as a reference (there are at least 2 offered):
// 2. copy assignment operator
person& operator=(const person& that)
{
char* local_name = new char[strlen(that.name) + 1];
// If the above statement throws,
// the object is still in the same state as before.
// None of the following statements will throw an exception :)
strcpy(local_name, that.name);
delete[] name;
name = local_name;
age = that.age;
return *this;
}
What I'm finding confusing is, why are they including the line delete[] name;
?
Here is the other example they provide:
person& operator=(const person& that)
{
if (this != &that)
{
delete[] name;
// This is a dangerous point in the flow of execution!
// We have temporarily invalidated the class invariants,
// and the next statement might throw an exception,
// leaving the object in an invalid state :(
name = new char[strlen(that.name) + 1];
strcpy(name, that.name);
age = that.age;
}
return *this;
}
I immediately shied away from this one because I couldn't understand why the function checks if(this != &that)
and then runs delete (delete[] name;) on an array that doesn't seem to have been generated yet. When the assignment operator is invoked, is the regular constructor called immediately before the copy assignment operator function is called? Therefore meaning that we must delete the array that was generated by the classes constructor because it can only be full of junk data?
Why can't I just write:
name = that.name
or
name = new char[that.name.size()];
for (int i = 0; i < that.name.size(); i++)`
{
name[i] = that.name[i]
}
This is probably really basic and I should just implement what the post suggests but my actual use-case involves an array of struct
s with multiple members and so Im just having a little bit of a difficulty understanding what exactly I need to do.
I realize there are like 2.5 questions here. Any insight would be appreciated.
This is my first time implementing custom copy constructors and copy assignment operators and Im sure it will seem easy after I've done it a few times.
Thanks in advance.
All of this is needed to make sure that memory is managed correctly.
If I understand correctly, you need an answer to when is operator=
actually called?
Well, operator=
is always called when two valid objects exist. One of them is being assigned to, and second one is source of data. After this operation, both object must still remain valid.
This means than inside operator=
you have this
object with memory allocated for a string (which was allocated in one of the contrusctors) and that
object, also with memory allocated for another string.
What I'm finding confusing is, why are they including the line
delete[] name;
?
We have to first clean up memory which is currently residing in this
object, or else we would lose this pointer after assigning new momry to it.
When the assignment operator is invoked, is the regular constructor called immediately before the copy assignment operator function is called?
No. The object already exists, that's why operator=
is called (and not a copy constructor).
Therefore meaning that we must delete the array that was generated by the classes constructor because it can only be full of junk data?
It is full of valid data. Your object was contructed, it has some data in it, and then you assign something else to this object.
Addendum: When is copy contructor called and when is operator=
called? (see this question for more detailed information):
class A{};
int main()
{
A a1; //default constructor
A a2 = a1; //copy-constructor, not assignment operator! New object is needed
A a3;
a3 = a1; //we have valid and existing object on the left side, assignment operator is used
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With