What does the default constructor do when it is empty?

I wonder if anyone can explain what ctor does by default after allocating memory, how does it initialize allocated memory?

+4
source share
3 answers

I don’t know what language you asked the question from, but I will still try to answer in C ++ and Java

In C ++, it:

  • leaves built-in types ( int , float , pointers, etc.) to an uninitialized value
  • calls the default constructor for class members

In Java, I think that all members of the class are initialized to the default value (0 or NULL).

+5
source

Default constructors refer to default constructors for all non-static data members, with the exception of built-in types that remain uninitialized.

[2003: 12.1/5] :

The default constructor for class X is the constructor of class X, which can be called without an argument. If the user does not declare a constructor for class X, the default constructor is declared implicitly.

[2003: 12.1/8] :

The default constructors are called implicitly to create objects of the static or automatic storage duration class (3.7.1, 3.7.2), defined without an initializer (8.5).

+6
source

Take a look at this (in the implementation of C and C ++).

Yes, implementation varies from language to language.

+1
source

All Articles