简体   繁体   中英

About constructor call for base and derived class

I am new in C++ and I have just started studying polymorphism. I know that if I create an object for the derived class then the constructor of both derived and base class is get called. Does it mean that, when I create an object for the derived class, eventually I am ending up with two objects- one is created by the constructor of base class and another one created by the constructor of derived class?

Can anyone explain, what is the job of base class constructor when I want to create an object for the derived class.

The job of the base class constructor is to initialise the base class member variables (consider the case of a private member variable in the base class).

When you call a constructor for a derived object, you only end up with one object. The base class constructor initialises the base class parts of the new object, and the derived constructor initialises the derived class parts of the same new object.

Constructors do not allocate space and initiate instances of objects; they initialise the object immediately after space has been allocated.

When you declare a object on the stack or use new first the memory is reserved and object is created, then the constructors are executed, starting with the base constructor and working upwards towards the most derived class.

You will end up with a derived class object, that contains a base class object.

Constructors don't magically create another instance of an object. They initialise a certain piece of memory, but calling a constructor and allocating memory are not the same thing -- the latter is either done behind the scenes (for objects with automatic and static storage duration) or with new and malloc (for objects with dynamic storage duration).

EDIT: Before I get angry comments about it: "behind the scenes" is a vague way to put it; the definition of an object with automatic or static storage duration ensures that it gets memory.

Whether you are creating an object of the base class or derived class , you end up with one object.

Just because the base class constructor is called, it doesn't mean you get one extra object for that call. Here, the b ase class constructor will be executed, which typically sets attributes of the base class. The object on the whole would be composed of the base class properties and derived class properties.

当创建派生类的对象时,首先创建该类的构造函数,然后使用基类的构造函数,在调用它时将由一个单独的对象调用它将被该对象调用。

If you have a class then you would have to define a contructor or constructors (there are certain circumstances in where you do not have to). A constructor has the purpose to initialise the class data members, you give a value to its data members. Any data member that you do not give it a value, will have an undefined value unless the data member is an instance of a class that happens to have a default constructor.

So when you create an instance of that class, the compiler will invoke the appropriate constructor, and the members of the instance that you have just created will have data initialised with values as you set them in the constructor.

If you have a derived class, you can add data members to it, but remember that it has also an inherited member: the base class. So when you define a constructor in you derived class, you must invoke a constructor of the base class (so that the members of the base class get fully initialised) and assign value to the derived class own data members.

So you create an instance of the derived class, the derived class constructor is invoved. As part of its functionality it invokes the constructor of its base class (so you might say two constructors but this is the wrong of viewing it).

If you create an instance of the base class, only that constructor is invoked.

When we create an object, Constructor will be called. Its not when constructor called object is being created.

You can assume Creating object as a function and Constructor is another function which is called inside it.

Assume A is a base class and B is a derived class,

class A {

}

class B : public A {

}

We will be creating object for class A in below ways,

A obja;

or 

A obja = new A();

In both cases you can assume a function Create is getting called,

A::Create()
{
   A();
}

If you are creating Derived class B object then Create method of B will be called,

So internally Create method of B will look like below,

B::Create()
{
  B();
  A();
}

So when we create Derived class object first derived class initialization happens and then Base class initialization happen. It not means object is created twice.

Object will be created for the Call where you are creating object using.

B obj;

or

B obj = new B();

Calling the constructors are internal functionality as Constructors called in the Create function.

Note : Create function in the above code is done only for illustrative purpose.

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