What is the benefit of constructors in C++?

13,155

Solution 1

The constructor is the way to have every object determine its initial value. When you create a new object in C++, initially all of its data members have a completely indeterminate value. If you want to have the object determine its own default, it needs to have some code to execute in order to set its fields to meaningful values. So in short, yes, objects should determine their initial values, and the constructor is the way to do it. They're invoked automatically, and so from a client's perspective there's no need to explicitly invoke any initialization routines. The constructor does this automatically.

As for the default constructor, it can and often does have code in it that makes it very different from nothing. A vector constructor, for example, might set up a pointer to a small buffer where the elements can be stored, as well as recording the size of the object as zero. Doing nothing would leave the pointer pointing somewhere randomly in memory and with the size fields set to garbage, violating the class invariants and rendering the object unusable.

Solution 2

In C++ there is a discrepancy with initialization (due to it being mostly C-compatible).

C++ types (classes) can (should) provide constructors to initialize themselves. The default constructor simply provides default behaviour.

C types (int, char) cannot have constructors - the always need to be initialized manually.

If you implement a constructor, all member variables of C types (int, char etc.) need explicit initialization. Otherwise their content is undefined. Member variables of a C++ type (class) will initialize themselves with their default constructor. If this is not suitable for your case, use a dedicated non-default constructor with arguments.

A default constructor is a way of letting of letting every object determine its initial value. It is just that you need a constructor for that.

Solution 3

Some classes need data in order to be able to initialise themselves, they can't always determine this data for themselves. Hence passing parameters via a constructor is a way to make that initial data available. Just consider a simple string class, it's really convenient to create a string with an initial value via it's constructor.

Solution 4

The intent is very much that, once initialised, the class object is 'ready to use'. With simple, built-in types, the compiler knows how to initialise them – 0 for instance. But, with more complicated types, particularly involving inheritance and reference data, this becomes difficult or even impossible.

C++ requires a reference variable to be initialised at creation (construction for a class) and this has to be done in a constructor initialiser list, for example:

class Demo {
private:
    int& refInt;
public:
    //Demo() {} // illegal - will not compile since it does not intiailise refInt
    Demo(int anInt) : refInt(anInt) {} // valid, correctly initialised refInt
};

int main() {
    int a;
    Demo demo(a);

    return 0;
}

Note: in this example you cannot have a default constructor since you must initialise refInt.

In addition, for a class that inherits, the constructor is the place to define which constructor in the base class (or classes) is called during initialisation of the derived class.

Solution 5

And, also, what is the benefit of the default constructor? At the end, it is doing nothing, isn't it?

It seems that you are confusing a default constructor with a compiler-generated default constructor. The former is just any constructor that can be invoked with no arguments, which means that it either has no arguments at all, or all the arguments have default values. The latter is generated when there are no explicitly declared constructors and is equivalent to an explicitly defined constructor with an empty body and empty initializer list (which implies that the base class must also have a default constructor).

Now, even if the default constructor is compiler-generated or has empty body, it is wrong to say that it's doing nothing. It does at least three things that I can remember right away:

  1. It calls the default constructor of the base class.
  2. It initializes the vtable pointer for polymorphic classes.
  3. It calls the default constructors of the non-POD fields.

And only when the class has no base class, is non-polymorphic and has only POD fields, the compiler-generated constructor does literally nothing. But it is still important to have because base classes need something to call.

Share:
13,155
Simplicity
Author by

Simplicity

Updated on June 29, 2022

Comments

  • Simplicity
    Simplicity almost 2 years

    Constructors initialize the data members of a class when an object is created.

    My question is, what is the benefit of this initialization process? Why wouldn't we let every object determine its initial value without a constructor to be called?

    And, also, what is the benefit of the default constructor? At the end, it is doing nothing, isn't it?

    Thanks.

  • MSalters
    MSalters over 13 years
    The part about C types is basically wrong. It misses bool which is a C++ type. The "cannot have constructors" part is meaningless. The correct statement is that they (by definition) do not have constructors. Also, they do not always need to be initialized (globals and statics are the exception).
  • sstn
    sstn over 13 years
    Ok - bool is indeed a C++ type - didn't get a proper name for non-class types. I thought about the global/static issue of zero-initialization, but didn't consider it relevant for the current problem.