Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Who deletes the copied instance in + operator ? (c++)

I searched how to implement + operator properly all over the internet and all the results i found do the following steps :

const MyClass MyClass::operator+(const MyClass &other) const
{
    MyClass result = *this;  // Make a copy of myself. Same as MyClass result(*this);
    result += other;         // Use += to add other to the copy.
    return result;           // All done!
}

I have few questions about this "process" :

  1. Isn't that stupid to implement + operator this way, it calls the assignment operator(which copies the class) in the first line and then the copy constructor in the return (which also copies the class , due to the fact that the return is by value, so it destroys the first copy and creates a new one.. which is frankly not really smart ... )

  2. When i write a=b+c, the b+c part creates a new copy of the class, then the 'a=' part copies the copy to himself. who deletes the copy that b+c created ?

  3. Is there a better way to implement + operator without coping the class twice, and also without any memory issues ?

thanks in advance

like image 479
OopsUser Avatar asked May 19 '10 13:05

OopsUser


People also ask

Does destructor invoke delete?

For objects of class type, the name of the deallocation function is resolved in global scope if the delete expression begins with the unary scope resolution operator ( :: ). Otherwise, the delete operator invokes the destructor for an object prior to deallocating memory (if the pointer is not null).

When should you delete copy constructor?

Copy constructor (and assignment) should be defined when ever the implicitly generated one violates any class invariant. It should be defined as deleted when it cannot be written in a way that wouldn't have undesirable or surprising behaviour.

What is delete operator in C?

Delete is an operator that is used to destroy array and non-array(pointer) objects which are created by new expression. Delete can be used by either using Delete operator or Delete [ ] operator. New operator is used for dynamic memory allocation which puts variables on heap memory.

What is a deleted operator?

The delete operator removes a given property from an object. On successful deletion, it will return true , else false will be returned.


1 Answers

  1. That's effectively not an assignment operator, but a copy constructor. An operation like addition creates a new value, after all, so it has to be created somewhere. This is more efficient than it seems, since the compiler is free to do Return Value Optimization, which means it can construct the value directly where it will next be used.

  2. The result is declared as a local variable, and hence goes away with the function call - except if RVO (see above) is used, in which case it was never actually created in the function, but in the caller.

  3. Not really; this method is much more efficient than it looks at first.

like image 72
David Thornley Avatar answered Oct 26 '22 14:10

David Thornley