If you do not declare any constructor yourself, C ++ compilers will always create a public trivial constructor for you. Moreover, it also implicitly creates a public copy constructor and assignment operator.
From C ++ 11 standard 12.1.5:
If there is no constructor declared by the user for class X, a constructor without parameters is implicitly declared as default. An implicitly declared default constructor is an inline public member of its class.
and 12.8.7, 12.8.11:
If the class definition does not explicitly declare the copy constructor, it is declared implicitly. [...] An implicitly declared instance [...] constructor is an inline public member of its class.
and finally 12.8.18, 12.8.20, 12.8.22:
If the class definition does not explicitly declare the copy assignment operator, one is declared implicitly. [...] If the definition of class X does not explicitly declare a move assignment statement, then it will be implicitly declared [...]. The implicitly declared assignment operator copy / move is an embedded public member of its class.
Please note that the motion assignment operator is generated only under certain circumstances that are beyond the scope of this question, see 12.8.20 for more details.
If you need a private constructor, you must declare it yourself:
class my { my() {} };
If you want to prevent the creation of a copy constructor or assignment operator, you can either declare but not implement them:
class my { my(my const &); };
Or, since C ++ 11 explicitly removes them:
class my { my(my const &) = delete; };
nijansen
source share