Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it correct to use declaration only for empty private constructors in C++?

For example is this correct:

class C 
{
   private: 
     C();
     C(const & C other);
}

or you should rather provide definition(s):

class C 
{
   private: 
     C() {};
     C(const & C other) {};
}

? Thanks for the current answers. Let's extend this question - does compiler generate better code in one of this examples? I can imagine that providing body for ctor forces compiler to include some (empty) code in compilation unit? Is this also true for auto-generated code?

like image 406
mip Avatar asked Nov 28 '22 12:11

mip


1 Answers

If you do not wish your object to be copyable, then there is no need to provide the implementation. Just declare the copy ctor private without any implementation. The same holds for other ctors, if you do not want any body to use them, just declare them private without any implementation.

like image 81
Naveen Avatar answered Dec 10 '22 23:12

Naveen