简体   繁体   中英

Regarding Assignment Operator Overload

I am trying to overload the assignment operator and trying to use the "copy and swap" idiom to avoid memory leaks. I have implemented my own default constructor and copy constructor. I just need to know when in my main I say a=b and assignment operator gets called, then what happens at the parameter part of (line 2). I only want to know about the actions that occurred at the parameter part .

template <class T>
Mystack<T> & Mystack<T>::operator=(Mystack<T> source) // Line 2, 
//Passing by value intentionally  

Option 1 : Default Constructor gets called and whatever code is mentioned in default constructor gets executed on source .

Option 2 : Default Constructor gets called and copy by value occurs and b's value gets copied to source .

Option 3 : Copy Constructor gets called and copy by value occurs and source gets the same value as it was in b .

If none of the above is true kindly let me know what's happening actually at the parameter part .

template <class T>
Mystack<T> & Mystack<T>::operator=(Mystack<T> source)

That assignment-operator gets its argument by value , which means the caller passes a fresh Mystack<T> , however that gets constructed.

As you implemented your own copy-constructor and no move-constructor, that one will certainly be called if not constructing in-place.

Nothing will be called on source by the call, untill you return and the argument gets destroyed by a call to the destructor at the end of the full expression.

Some examples:

using A = MyStack<T>;
A make_a();
A a, b;
a = b; // Calls the copy-ctor to construct the argument to op=
a = A(); // Directly constructed as the argument
a = make_a(); // Return-value is directly used as the argument

Passing by-value allows the creation of a new object. The way this object is created depends on the type of the expression it is initialized with. If the argument is an lvalue then the copy-constructor is called, and likewise if it is an rvalue then the move-constructor is called.

Both of these functions are provided to you by the compiler given you don't provide them yourself and don't declare any special member functions that would inhibit their creation (among other conditions).

MyStack<int> a, b;
a = b;            // Calls MyStack(MyStack const&)
a = std::move(b); // Calls MyStack(MyStack&&)

The purpose of taking by-value is to have a completely new object. This can be done in two ways - by copying the old object to the new one or exchanging ownership with an object that is no longer needed.

On the a = b line, b 's contents are copied to the parameter source . a = std::move(b) exchanges ownership with b by removing its "guts" and giving them to a . a now owns whatever b used to have, and b 's internals are left in a state that resemble a moved-from object..

Note that initializing a parameter by value is copy-initialization , and despite its name the move-constructor may still be used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM