Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are empty constructors always called in C++?

I have a general question, that may be a little compiler-specific. I'm interested in the conditions under which a constructor will be called. Specifically, in release mode/builds optimised for speed, will a compiler-generated or empty constructor always be called when you instantiate an object?

class NoConstructor  
{  
    int member;  
};  

class EmptyConstructor  
{  
    int member;  
};

class InitConstructor  
{  
    InitConstructor()  
        : member(3)   
    {}  
    int member;  
};

int main(int argc, _TCHAR* argv[])  
{  
    NoConstructor* nc = new NoConstructor(); //will this call the generated constructor?  
    EmptyConstructor* ec = new EmptyConstructor(); //will this call the empty constructor?  
    InitConstructor* ic = new InitConstructor(); //this will call the defined constructor  

    EmptyConstructor* ecArray = new EmptyConstructor[100]; //is this any different?
}

I've done a lot of searching, and spent some time looking through the generated assembly code in Visual Studio. It can be difficult to follow in release builds though.

In summary: Is the constructor always called? If so, why?

I understand this will very much depend on the compiler, but surely there's a common stance. Any examples/sources you can cite would be really appreciated.

like image 541
JBeFat Avatar asked Feb 23 '11 21:02

JBeFat


People also ask

Is Empty constructor default?

Providing an empty default constructor( private ) is necessary in those cases when you don't want an object of the class in the whole program. For e.g. the class given below will have a compiler generated default empty constructor as a public member . As a result, you can make an object of such a class.

Which constructors are always called automatically?

Static constructors are called automatically, immediately before any static fields are accessed, and are generally used to initialize static class members.

What is called Empty constructor?

In computer programming languages, the term default constructor can refer to a constructor that is automatically generated by the compiler in the absence of any programmer-defined constructors (e.g. in Java), and is usually a nullary constructor.

Can a constructor have empty body?

An empty constructor is required for each class. If you want to have a constructor with initialization logic, you can add it along with the empty constructor. In some languages, if you do not write an empty constructor explicitly, compiler will generate it for you.


2 Answers

will a compiler generated constructor/empty constructor always be called when you instantiate an object?

No. If your class is a so-called “POD” (plain old data) then the compiler-generated constructor won’t always be called.

Specifically, it won’t be called in the two following cases:

struct Pod {
    int x;
};

int main() {
    Pod pod;
    std::cout << pos.x << std::endl; // Value undefined.

    Pod pod2 = Pod(); // Explicit value initialization.


    Pod* pods = new Pod[10];
    // Values of `pods` undefined.

    Pod* pods2 = new Pod[10](); // Explicit value initialization.
}

The conditions for when exactly a type is a POD are a bit tricky. The C++ FAQ has a nice breakdown.

like image 69
Konrad Rudolph Avatar answered Sep 24 '22 17:09

Konrad Rudolph


Logically, the constructor is called. In generated code, if the constructor does nothing, there will be no instructions that can be traced back to the constructor, unless your compiler is very very bad at optimizing and inserts a call to something that just returns.

like image 30
Erik Avatar answered Sep 23 '22 17:09

Erik