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.
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.
Static constructors are called automatically, immediately before any static fields are accessed, and are generally used to initialize static class members.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With