By default, the constructor of the empty class is public. But how?

I have a simple question:

class my { }; my ob; 

The compiler allows me to create an object that makes sense. And I know that you cannot create an object where the constructor is private.

It seems to me that everything inside the class is private , but it is clearly not the default constructor (because it allows me to create an object, since the default constructor should be public ). But what confuses me is that there is no public section in the class.

So, does the public section only create the default constructor in it?

Or is something else happening and my rationale is wrong?

In addition, how does access to the public, private and protected internally organized / tracked during the creation or access of the property take place?

I got this question since so far I have never created an object of an empty class.

+7
c ++ private constructor default-constructor public-method
source share
4 answers

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; }; 
+6
source share

Yes, the compiler will create a default constructor, and the default constructor and default assignment constructors will be "public", because everything else will make the class useless ...

Of course, these constructors would be quite simple - in fact, you can replace it with "nothing", since building an empty class will do nothing.

+1
source share

The default constructor created by the compiler (and other operators) is automatically published. If you want the default constructor to be private, you need to specify this yourself by declaring it in a private section of your class.

The concepts of private, protected and public relate only to the compiler. They are irrelevant and are not tracked at runtime.

+1
source share

The compiler will generate the default constructor as inline public , if it is not defined by the user, the corresponding section is C++ draft 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 (8.4). An implicitly declared default constructor is an inline public member of its class.

+1
source share

All Articles