Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do reference type members cause implicitly-declared copy assignment operator to be deleted

Tags:

c++

From CPP Reference:

Deleted implicitly-declared copy assignment operator The implicitly-declared or defaulted copy assignment operator for class T is defined as deleted in any of the following is true:

T has a non-static data member that is const T has a non-static data member of a reference type. T has a non-static data member that cannot be copy-assigned (has deleted, inaccessible, or ambiguous copy assignment operator) T has direct or virtual base class that cannot be copy-assigned (has deleted, inaccessible, or ambiguous move assignment operator) T has a user-declared move constructor T has a user-declared move assignment operator  

So that tells me what causes the deletion but not the why? Can anyone explain for:

T has a non-static data member of a reference type. 

and whether this will suffice in my class to deal with the deleted operator:

T& T:operator=(T& t){}; 

if I have a member of a base class which is a reference type.

Do I need to do anything in my operator= such as explicitly declare return *this or will the compiler (g++) handle this for me? Do I have to do anything special with the reference member? Sorry for noob question but I am new to C++ having started off with managed languages (C# and Java).

like image 912
Luthervd Avatar asked Nov 15 '14 13:11

Luthervd


People also ask

Is implicitly-declared as deleted?

The implicitly-declared or defaulted copy assignment operator for class T is defined as deleted in any of the following is true: T has a non-static data member that is const. T has a non-static data member of a reference type.

What is implicitly deleted copy constructor?

Deleted implicitly-declared copy constructorT has direct or virtual base class that cannot be copied (has deleted, inaccessible, or ambiguous copy constructors);

What happens if you don't define a copy assignment operator?

If no user-defined copy assignment operators are provided for a class type (struct, class, or union), the compiler will always declare one as an inline public member of the class.

What is a copy assignment operator?

Copy constructor. Assignment operator. It is called when a new object is created from an existing object, as a copy of the existing object. This operator is called when an already initialized object is assigned a new value from another existing object. It creates a separate memory block for the new object.


1 Answers

References are bound to an object when they are initialized and can never be altered after that, everything else you do to them affects the object they are bound to, not the reference itself.

So a reference member is set during construction, and never altered. Since the purpose of an assignment operator is to alter members after construction, it doesn't make sense to generate an implicit assignment operator when one of the member can never be altered. The compiler refuses to try and guess what you want it to do and forces you to provide your own assignment operator with the semantics you want.

Do I need to do anything in my operator= such as explicitly declare return *this or will the compiler (g++) handle this for me?

You absolutely definitely 100% need to return *this;

The only time you don't need an explicit return in C++ is if your function returns void or in main() (where there is an implicit return 0; if you reach the end of the function) or in unusual cases such as functions that never return (either looping forever or throwing an exception).

Do I have to do anything special with the reference member?

It depends what semantics you expect assignment of your type to have.

If you don't want it to change the object the reference is bound to, fine, do nothing with it.

If you want assignment to alter the object the reference is bound to, you need to do that.

If you want the reference to be re-bound to a different object, you're out of luck, C++ doesn't allow that.

like image 106
Jonathan Wakely Avatar answered Sep 17 '22 22:09

Jonathan Wakely