Default constructor of an Empty Class is public. But how?
Asked Answered
Y

5

13

I have a simple question:

class my
{
};
my ob;

Compiler allows me to create an object which makes sense. And, I am aware that you can't create object where the constructor is private.

To me it looks that, everything inside the class is private but obviously not the default constructor(because it is allowing me to create the object as default constructor should be public). But what confuses me is that there is no public section in the class.

So, does it create a public section only to put a default constructor under it in this case?

Or there is something else going on and my rationale is incorrect?

Also, how are accesses public, private and protected internally organised/tracked when an object is created/accessed?

I got this question as I never created an object of an empty class until now.

Yurikoyursa answered 6/8, 2013 at 13:8 Comment(1)
The compiler provided constructors, destructor and assignment operators are public. You have to actively do stuff to disallow them.Privation
H
10

If you do not declare any constructor yourself, C++ compilers will always generate a public trivial constructor for you. More than that even, it will also implicitly create a public copy constructor and assignment operator.

From C++11 standard 12.1.5:

If there is no user-declared constructor for class X, a constructor having no parameters is implicitly declared as defaulted. 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 a copy constructor, one is declared implicitly. [...] An implicitly-declared copy [...] 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 a copy assignment operator, one is declared implicitly. [...] If the definition of a class X does not explicitly declare a move assignment operator, one will be implicitly declared [...]. An implicitly-declared copy/move assignment operator is an inline public member of its class.

Note that a move assignment operator will only be generated under certain circumstances, which are beyond the scope of this question, see 12.8.20 for more details.

If you want a private constructor you have to declare it yourself:

class my { my() {} };

If you want to prevent the generation of copy constructor or assignment operator you can either declare, but not implement them:

class my { my(my const &); };

Or, since C++11, explicitly delete them:

class my { my(my const &) = delete; };
Hix answered 6/8, 2013 at 13:13 Comment(0)
D
1

Yes, the compiler will produce the default constructor and the default copy constructor and default assignment operators as "public" - because anything else would make the class rather useless...

Of course, those constructors would be rather simple - in fact, it can be replaced with "nothing", since constructing an empty class will do nothing.

Diligence answered 6/8, 2013 at 13:11 Comment(0)
M
1

The compiler generated default constructor (and other operators) are automatically public. If you want the default constructor to be private then you need to specify this yourself my declaring it within a private section of your class.

The concepts of private, protected and public are only relevant to the compiler. They have no meaning and are not tracked at runtime.

Meng answered 6/8, 2013 at 13:11 Comment(0)
V
1

The compiler will generate the default constructor as inline public if it is not defined by the user, the relevant section of the C++ draft standard is 12.1/5:

If there is no user-declared constructor for class X, a constructor having no parameters is implicitly declared as defaulted (8.4). An implicitly-declared default constructor is an inline public member of its class.

Virga answered 6/8, 2013 at 13:16 Comment(0)
G
0

Usually compiler by default generates 4 things at the time of creation of object.

  1. Default constructor

  2. Copy constructor

  3. Copy assignment operator

  4. Destructor

For example:

class First {

      First(){}                        //default constructor
      First(const First &){}           //copy constructor
      First& operator=(const First&){  //Copy assignment operator
              return *this;
      }
      ~First(){}                       //Destructor  
}   

These are implicitly inline public member, unless there is no user declared constructor.

Garek answered 26/12, 2018 at 6:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.