answersLogoWhite

0


Best Answer

You cannot actually call any constructor, you can only invoke construction, either by instantiating a static instance of a class or by dynamically creating one with the new operator. In the case of base class construction via a derived class, the base class constructor is invoked by the derived class' initialisation list.

Every class constructor has an initialisation list whether you define one or not (the compiler will generate one automatically if you don't). When you derive one class from another, the derived class initialisation list invokes a call to the base class default constructor. The only exception is the compiler-generated copy constructor which automatically calls the base class copy constructor.

If you define your own initialisation list, then you can explicitly invoke any base class constructor overload, thus making your construction code all the more efficient. However, copy constructors should always invoke the base class copy constructor, so if you define a copy constructor, you must explicitly invoke the base class copy constructor -- the compiler will not invoke it implicitly from a user-defined copy constructor.

While many programmer's use the constructor's body to initialise a class, this is highly inefficient. Even if you don't specify an initialisation list, one is created for you, resulting in every base class and every member variable being initialised twice, which can quickly add up to a substantial cost in performance.

The constructor's body should only really be used for initialisation when it would be difficult or impossible to do so from the initialisation list. Remember that your object doesn't physically exist until initialisation is complete, so you may not have access to some members, particularly base class members, at certain points in the initialisation process.

Initialisation must be done from the ground up, starting with the base classes and ending with the actual class members, and all in the order they were declared. Note that only direct base classes (or virtual base classes) should be invoked from the initialisation list. The base classes themselves should invoke their own base class constructors, if they have any. Thus no matter which derivative you construct, the least-derived class is always constructed first.

User Avatar

Wiki User

10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the technical name for calling a base class constructor using derived class constructor?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Can a constructor be declared as virtual?

A constructor cannot be virtual because at the time when the constructor is invoked the virtual table would not be available in the memory. Hence we cannot have a virtual constructor. ## Constructor called implicitly not explicitly so constructor is not virtual.


Features of constructor of a derived class in c plus plus?

class base { base():m_data(0){} base(int i):m_data(i){} base(const base& b):m_data(b.m_data){} private: int m_data; }; class derived : public base { derived():base(){} // call base class default constructor derived(int i):base(i){} // call base class overloaded constructor derived(const derived& d):base(d){} // call base class copy constructor };


When invoking a constructor from subclass its super class's no-arg constructor is always invoked?

Implicitly: (i.e., you do not code for it, but works as if you did)calling the no-argument constructor of the subclass, and there is no explicitly "redirect" codes.Explicitly:a constructor with base() / super() in the implementation, even that invoked constructor required some arguments.C# example: public SubClass(string whatever) : base() {//...}


What is the method of constructor overloading in c plus plus?

Constructor overloading, just like any function's overloading, is where more than one configuration of parameters exists for the function. Based on the number and type of the parameters, different versions of the function can be resolved by the linker. This is typically used in the constructor as the default constructor (no parameters), the copy constructor (one reference parameter of the same type as the class), and the conversion constructor (any other combination of parameters).


Can you initialize an object without constructor in java?

No. if you wish to create an object that you plan on using in a java program then the answer is NO. You cannot initialize an object of a Java class without calling the constructor.

Related questions

Can a constructor be declared as virtual?

A constructor cannot be virtual because at the time when the constructor is invoked the virtual table would not be available in the memory. Hence we cannot have a virtual constructor. ## Constructor called implicitly not explicitly so constructor is not virtual.


When a derived class object is deleted which gets deleted first - the base class object or the derived class object?

When there is no further use of derived class obect in execution sequence then It gets deleted. calling of distructor sequence is reverse of constructor calling sequence ,so first derived class obect deleted than base class obect.


Can a derived class call the static constructor of a base class in .Net?

No. not directly. The static constructor is called by .net framework, not by your code. I always treat static constructor is the class initializer (not the instance one). The initialization routine is only called when the class definition being loaded. When the derived class is loaded, since this class derived from the base, it makes to go up the chain (of the hierarchy) to call those static constuctors. So, when a derived class is loaded, the static constructors of the hierarchy is called up. (any one of them had been loaded, the calling sequence stops). You cannot call those static constructor directly, nor you can skip "not to execute" them.


Can you have virtual constructor?

The short answer is no, you cannot. The long answer is that constructors are not functions that can be called directly, and overriding a base class constructor would have no practical meaning since the derived class is itself responsible for calling its own base class constructors (whether implied by omission or explicitly via the derived class' initialisation list). Even so, the derived class isn't calling the base class constructor directly (that's why constructors have no return value; the actual call is made behind the scenes). The base class itself may be derived in which case its base class must be constructed before it can be constructed. This is the complete reverse of how a virtual function behaves, and is the reason that destructors can be virtual but constructors cannot. When a base class is destroyed, all its derivatives must be destroyed first, starting with the most-derived class of object. This can only be achieved through virtual destruction.


Features of constructor of a derived class in c plus plus?

class base { base():m_data(0){} base(int i):m_data(i){} base(const base& b):m_data(b.m_data){} private: int m_data; }; class derived : public base { derived():base(){} // call base class default constructor derived(int i):base(i){} // call base class overloaded constructor derived(const derived& d):base(d){} // call base class copy constructor };


Give an example of constructor and destructor in derived class?

class superclass { public: superclass() {... } // c'tor public: virtual ~superclass() {... } // d'tor }; // superclass class derived: public superclass { public: derived() : superclass() { ... } // derived c'tor public: virtual ~derived() {... } // derived d'tor }; // derived class


When invoking a constructor from subclass its super class's no-arg constructor is always invoked?

Implicitly: (i.e., you do not code for it, but works as if you did)calling the no-argument constructor of the subclass, and there is no explicitly "redirect" codes.Explicitly:a constructor with base() / super() in the implementation, even that invoked constructor required some arguments.C# example: public SubClass(string whatever) : base() {//...}


What is dynamic constructor?

In Java classes we can declare multiple constructors. The JVM would dynamically decide which constructor to invoke based on the parameters passed from the calling class. Ex: public class Test { public Test(){ ... } public Test(String arg1){ ... } public Test(String arg1, int arg2){ ... } } In the above class, there are 3 different constructor declarations. Whenever a constructor is invoked from a calling class, the JVM would decide which one to invoke based on the number of arguments passed.


What is common constructor?

Type your answer here... when the constuctor of derived class is used to initialise the members of Base class such constructer is called common constructer.


What is the method of constructor overloading in c plus plus?

Constructor overloading, just like any function's overloading, is where more than one configuration of parameters exists for the function. Based on the number and type of the parameters, different versions of the function can be resolved by the linker. This is typically used in the constructor as the default constructor (no parameters), the copy constructor (one reference parameter of the same type as the class), and the conversion constructor (any other combination of parameters).


Can you initialize an object without constructor in java?

No. if you wish to create an object that you plan on using in a java program then the answer is NO. You cannot initialize an object of a Java class without calling the constructor.


What happens when no constructor function is declared in a class - in Java?

When any constructor is deffined in your class, the java compiler create a default no argument constructor for you. This constructor only have an invocation to the super class constructor (" super( ) ").