The default constructor in C ++

I just liked the question, but I could not find the answer on the Internet.

Suppose we have a simple heading:

// SimpleHeader.h class SimpleClass { int i; } 

As we know, a default constructor is automatically created for this class.

Now I have 2 more files:

 // First.cpp #include <SimpleHeader.h> // ... SimpleClass a; // ... 

and

 //Second.cpp #include <SimpleHeader.h> // ... SimpleClass b; // ... 

Both First.obj and Second.obj will contain code for the class

+7
source share
5 answers

From the standard: if you do not write any constructors, you will be provided with a default constructor, and this default constructor will be defined internally and is equivalent to the empty constructor T::T() {} .

I am sure that [edit] your built-in constructor will not actually lead to any machine codes at all.

+4
source

Yes, presumably, the compiler should generate code in both object files, if in the end they are not linked to each other. The linker then uses one definition rule to select one version and throws another when you link the two object files together with the binary executable.

+3
source

First of all, it depends on the compiler and many other circumstances.

There are 3 common scenarios.

  • By default, a constructor is generated and included in each of your First.obj and Second.obj object files, but when you combine them together to create an executable file, only one of them is used and included.

  • The constructor is built-in wherever you create the object (as a rule, only for simple constructors, where the compiler can simply reset the memory)

  • There is no need to create / call the default constructor. This can happen if you declare an object in a file area, and the object just needs to have zero zero memory โ€” the compiler can just put the object in a special region, zero initialization when the program starts โ€” and omit the default constructor call.

+3
source
Default constructor

nothing more than allocates space for an object. since these are not dynamic vars, memory is already allocated at the bootloader stage, and no more code is required.

More interesting would be what happens if you implement a complex constructor and dynamically allocate objects.
In this case, both obj files will have constructor code.

0
source

None. In C and C ++, you can declare many times, saying: there is code for this function, but it is somewhere else. You can determine only once and in the place where you defined it, where the code is generated in this obj file. So, you have three .cpp files and one header - the first file that defines the class, and the other two - its objects. The obj files for the other two files will not contain any code for the class, only some information that is sufficient for the linker to place calls in the code is the defining obj file.

If you define a class in two places by implicitly placing the method definitions in the header included in several files, the linker will not mind, because the definitions are โ€œthe sameโ€, they just appear in each object initially, and the final application will include only one of the default features.

You can always make as many instances of a class as you want, and the method code is never copied. It exists in one place for all different files, functions, and so on to use and create objects of this class.

Some default constructors may be smart and need some code, some for POD structures, for example, can be fully optimized and do not need any code. This is always the case, although creating more instances does not copy any functions, including constructors.

0
source

All Articles