3

I have read that the default constructor performs no action(The default constructor for class T is trivial (i.e. performs no action) if some conditions are true and those condition are true I guess when implicitly defined. http://en.cppreference.com/w/cpp/language/default_constructor

So if it performs no action, why is it even added by the compiler and why is it required for classes?

Thanks!

  • Why does f(x) = 0 require you to supply x? Because those are the rules of the language of math. It's still a function whether or not it does anything with the input, and we still talk in terms of functions, and we can still map the x/y. In C++ a constructor is required for classes as part of the language. Whether or not it does anything doesn't matter, something has to be there to get called. – Christopher Pisz May 19 '17 at 14:35
  • @ChristopherPisz, thank you. So what you are saying is that it is a rule of the language that there must be a constructor no matter if it performs any actions or not? Or am I misunderstanding something? –  May 19 '17 at 15:35
  • That's how I would answer on an interview. The CRT calls the constructor when a class is instantiated, so therefore one must exist for it to call, even if it does "nothing." Even if the body is empty, it is still at construction time that a class' member data is initialized. See https://isocpp.org/wiki/faq/ctors#overview-ctors for an in depth look at the topic of constructors. – Christopher Pisz May 19 '17 at 15:40
  • Possible duplicate of [What is the point of a C++ implicit default constructor?](http://stackoverflow.com/questions/39181981/what-is-the-point-of-a-c-implicit-default-constructor) – Amadeus May 19 '17 at 17:15

3 Answers3

2

It purpose is to bring object to life if there is no suitable constructor available

Example:

struct Foo{};

struct Bar
{
    Bar() = delete; // no default constructor
};

int main()
{
    Foo f;
//  Bar b;  // does not compile
}
Amadeus
  • 10,199
  • 3
  • 25
  • 31
  • But why can a class object only come to life if there is a constructor, even though the constructor does nothing? –  May 19 '17 at 15:37
  • 1
    @JakeBlandon If there was no concept of a default constructor, how would you make an object that cannot be default constructed? I think that's the point made here. – Rakete1111 May 19 '17 at 15:38
2

The CRT calls the constructor when a class is instantiated, so therefore one must exist for it to call, even if it does "nothing." The compiler generates a default constructor for you if one is not provided for that reason. Otherwise, there would be no way to instantiate the object. The rules, steps, and order of things that happen at construction time is simply part of the language.

Even if the body is empty, it is still at construction time that a class' data members are initialized.

Also, consider what occurs if the class is derived from another, at construction time.

See http://isocpp.org/wiki/faq/ctors#overview-ctors for an in depth look at the topic of constructors.

Christopher Pisz
  • 3,757
  • 4
  • 29
  • 65
  • Hmm thank you, I think I slowly get the point.. So are classes programmed to always call a constructor and thereby a constructor must be defined? Or? –  May 19 '17 at 16:48
1

Because the compiler will only generate a default constructor when it needs to be called. This could be by a derived class or a standard library container, for example. The compiler is still free to optimise it away as it is with an explicitly defined do nothing function.

Paul Evans
  • 27,315
  • 3
  • 37
  • 54