You get a bonus - 1 coin for daily activity. Now you have 1 coin

3.5. Overload assignment operation

Lecture



The operation has three features:
· The operation is not inherited;
· The operation is defined by default for each class as a bit-by-bit copy operation of the object to the right of the operation sign into the object to the left.
· An operation can be overloaded only in the class definition area. This ensures that the first operand is always a left-handed expression.

If you are satisfied with the bit-wise copying, it makes no sense to create your own function operator = (). However, there are cases when a bit-wise copying is undesirable. For example, using a predefined assignment operation for classes that contain pointers as component data most often leads to errors. Let's show it by example.

Custom class - string:

class string
{
char * p; // pointer to string
int len; // current string length
public:
string (char *);
~ string ();
void show ();
};
string :: string (char * ptr)
{len = strlen (ptr);
p = new chat [len + 1];
if (! p) {cout << ”Memory allocation error \ n”);
exit (1);}
strcpy (p, ptr);}
string :: ~ string ()
{delete [] p;}
void string :: show ()
{cout << * p << ”\ n”;}
void main ()
string s1 (“This is the first string”),
s2 (“And this is the second line”);
s1.show; s2.show;
s2 = s1; // This is an error s1.show; s2.show;
}

What is the mistake here? When the object s1 is assigned to the object s2, the pointer p of the object s2 begins to point to the same memory area as the pointer p of the object s1. Thus, when these objects are deleted, the memory pointed to by the pointer p of the object s1 is released twice, and the memory to which the pointer p of the object s2 pointed to before assignment is not released at all.

Although in this example this error is not dangerous, in real programs with dynamic memory allocation it can cause the program to crash.

In this case, it is necessary to overload the assignment operation. We show how to do this for our string class.

class string
{
char * p; // pointer to string
int len; // current string length
public:
...
string & operator = (string &);
};
string & string :: operator = (string & ob);
{if (this == & ob) return * this;
if (len <ob.len) {// additional memory is required
delete [] p;
p = new char [ob.len + 1];
if (! p) {cout << ”Memory allocation error \ n”);
exit (1);}
len = ob.len;
strcpy (p, ob.p);
return * this;}

In this example, it turns out that self-assignment does not occur (such as ob = ob). If self-assignment takes place, then the object reference is simply returned. It then checks whether there is enough memory in the object to the left of the assignment sign for the object to the right of the assignment sign. If not enough, then the memory is released and a new, required size is allocated. Then the string is copied to this memory.

Note two important features of the operanor = function.

First , it uses the reference parameter. This is necessary to prevent the creation of a copy of an object passed through a parameter by value. In cases of creating a copy, it is deleted by calling the destructor at the completion of the function. But the destructor frees the memory pointed to by p. However, this memory is still needed by the object, which is the argument. The link parameter helps to solve this problem.

Secondly , the function operator = () does not return an object, but a link to it. The meaning of this is the same as when using the reference parameter. The function returns a temporary object that is deleted after its completion. This means that a destructor will be called for a temporary variable, which frees the memory at address p. But it is necessary for assigning a value to an object. Therefore, to avoid creating a temporary object, a reference is used as the return value.

Another way to solve the problems described above is to create a copy constructor. But the copy constructor may not be as effective a solution as a link as a parameter and a link as the return value of a function. This is because the use of the link eliminates the resource costs associated with copying objects in each of the two specified cases.


Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

C ++ (C plus plus)

Terms: C ++ (C plus plus)