Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the distinction between implicitly-declared and implicitly-defined copy constructors?

I am reviewing the cppreference page on copy constructors here: http://en.cppreference.com/w/cpp/language/copy_constructor

I've read the 2 sections regarding implicitly-declared copy constructors and implicitly-defined copy constructors quite a few times but I still don't understand the distinction. Wouldn't an implicitly declared but NOT defined constructor result in linker problems?

The rules are very complex. I don't remember there being a distinction in C++03: Either you had a compiler generated copy constructor or you didn't.

Can someone explain (in simpler words) what the distinction/differences are between these two categories?

like image 809
void.pointer Avatar asked Oct 01 '14 19:10

void.pointer


People also ask

What is an implicitly declared constructor?

implicit constructor is a term commonly used to talk about two different concepts in the language, the. implicitly declared constructor which is a default or copy constructor that will be declared for all user classes if no user defined constructor is provided (default) or no copy constructor is provided (copy).

What is copy constructor when it is used implicitly for what purpose?

Copy constructor is used to initialize the members of a newly created object by copying the members of an already existing object. Copy constructor takes a reference to an object of the same class as an argument.

What is the difference between copy constructor and move constructor?

If any constructor is being called, it means a new object is being created in memory. So, the only difference between a copy constructor and a move constructor is whether the source object that is passed to the constructor will have its member fields copied or moved into the new object.

What is copy copy constructor?

The copy constructor is a constructor which creates an object by initializing it with an object of the same class, which has been created previously. The copy constructor is used to − Initialize one object from another of the same type.


1 Answers

This is clarified in a note in the standard at the beginning of clause 12:

[ Note: The implementation will implicitly declare these member functions for some class types when the program does not explicitly declare them. The implementation will implicitly define them if they are odr-used (3.2). See 12.1, 12.4 and 12.8. — end note ]

Normative references for C++14 (N3936) are 12.1/5, 12.4/6, 12.8/13, 12.8/26. In each case the corresponding special member function is implicitly defined if it is defaulted and not defined as deleted, and either odr-used or explicitly defaulted. If we have something like

struct Foo {};

and no objects of type Foo are ever created, all six special member functions (default constructor, destructor, copy constructor, move constructor, copy assignment operator, move assignment operator) are implicitly declared as defaulted, but not defined since they are not odr-used.

like image 84
Brian Bi Avatar answered Sep 18 '22 00:09

Brian Bi