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?
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).
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With