I am trying to understand the behaviour of auto-generated compiler code for various functions such as:
Will declaring them with "= default" cause any functional difference compared to non-declared case? Does the answer of this question differ among the above listed functions? If there is no functional difference, what are the consequences of using either case?
Copy constructor declared with "= default"
class MyClass
{
public:
MyClass();
MyClass(MyClass &other) = default;
OtherClass some_member;
};
Copy constructor not declared:
class MyClass
{
public:
MyClass();
OtherClass some_member;
};
In some cases the copy constructor is deleted by default. The most simplest example:
class myClass {
public:
myClass();
myClass(myClass &&);
};
myClass a;
void func()
{
myClass b=a; // ERROR
}
Rather than explaining the reason for the compilation error, I'll just paste the compilation error, verbatim, from my compiler:
‘constexpr myClass::myClass(const myClass&)’ is implicitly declared as deleted because ‘myClass’ declares a move constructor or move assignment operator
Explicitly declaring a default or a user-defined copy constructor will make the code compile.
There are also several other reasons. An explicitly declared copy constructor deletes the implicitly declared move constructor, for one.