简体   繁体   中英

Default constructor in C++

I was just curious about the question, but couldn't find the answer in the Internet.

Let's suppose we have simple header:

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

As we know, the default constructor is automatically generated for this class.

Now I have 2 more files:

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

and

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

Will both First.obj and Second.obj contain code for the class

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

I'm pretty sure that [edit]your thus inlined constructor will not actually result in any machine code at all.

Yes, presumably the compiler has to generate the code in both object files in case they weren't linked together eventually. The linker then uses the one definition rule to pick one version and throw the other one away when you link the two object files together into an executable binary.

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

Here's 3 common scenarios.

  1. The default constructor is generated and included in each of your First.obj and Second.obj object files, but when you're linking them together to produce the executable, only one of them used and included.

  2. The constructor is inlined everywhere you create an object (typically only for simple constructors, places where the compiler can just zero out the memory)

  3. There's no need to generate/call a default constructor. This might happen if you declare an object at file scope and the object just needs to have its memory zero initalized - the compiler might just place the object in a special region that is zero initialized at program startup - and omit calling the default constructor entierly.

default constructor is not more than allocating space for the object. since it's not a dynamic vars there memory is already been allocated in the loader stage and no more code is needed.

More interesting would be what would happen if you implemented a complex constructor and dynamically allocate the objects.
In that case, both obj files will have the constructor code.

Neither. In C and C++, you can declare something lots of times, saying, there is code for this function, but it's somewhere else. You can only define once, and in the place where you defined it, that's where the code is generated, in that obj file. So, you have three .cpp files, and one header, the first file defining the class, the other two making objects of it. The obj files for the other two files will not contain any code for the class, just some information which is sufficient for the linker to put in calls to the code is the defining file's obj.

If you define the class in two places, by putting method definitions implicitly in a header included in multiple files, the linker won't mind, because the definitions are 'the same', they just happen to appear in each obj initially, and the final application will only include one of the one default functions generated.

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

Some default constructors may be clever and need some code, some for POD structs for example may be optimised out entirely and not need any code. It's always the case though that making more instances doesn't copy any functions, including constructors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM