Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the rules for calling the base class constructor?

What are the C++ rules for calling the base class constructor from a derived class?

For example, I know in Java, you must do it as the first line of the subclass constructor (and if you don't, an implicit call to a no-arg super constructor is assumed - giving you a compile error if that's missing).

like image 665
levik Avatar asked Sep 23 '08 13:09

levik


People also ask

How do you call a base class constructor?

To call the parameterized constructor of base class inside the parameterized constructor of sub class, we have to mention it explicitly. The parameterized constructor of base class cannot be called in default constructor of sub class, it should be called in the parameterized constructor of sub class.

How do you call a base constructor in Java?

A derived Java class can call a constructor in its base class using the super keyword. In fact, a constructor in the derived class must call the super's constructor unless default constructors are in place for both classes.

Do you have to call base class constructor?

If a class do not have any constructor then default constructor will be called. But if we have created any parameterized constructor then we have to initialize base class constructor from derived class. We have to call constructor from another constructor. It is also known as constructor chaining.


2 Answers

Base class constructors are automatically called for you if they have no argument. If you want to call a superclass constructor with an argument, you must use the subclass's constructor initialization list. Unlike Java, C++ supports multiple inheritance (for better or worse), so the base class must be referred to by name, rather than "super()".

class SuperClass {     public:          SuperClass(int foo)         {             // do something with foo         } };  class SubClass : public SuperClass {     public:          SubClass(int foo, int bar)         : SuperClass(foo)    // Call the superclass constructor in the subclass' initialization list.         {             // do something with bar         } }; 

More info on the constructor's initialization list here and here.

like image 158
luke Avatar answered Oct 21 '22 16:10

luke


In C++, the no-argument constructors for all superclasses and member variables are called for you, before entering your constructor. If you want to pass them arguments, there is a separate syntax for this called "constructor chaining", which looks like this:

class Sub : public Base {   Sub(int x, int y)   : Base(x), member(y)   {   }   Type member; }; 

If anything run at this point throws, the bases/members which had previously completed construction have their destructors called and the exception is rethrown to to the caller. If you want to catch exceptions during chaining, you must use a function try block:

class Sub : public Base {   Sub(int x, int y)   try : Base(x), member(y)   {     // function body goes here   } catch(const ExceptionType &e) {     throw kaboom();   }   Type member; }; 

In this form, note that the try block is the body of the function, rather than being inside the body of the function; this allows it to catch exceptions thrown by implicit or explicit member and base class initializations, as well as during the body of the function. However, if a function catch block does not throw a different exception, the runtime will rethrow the original error; exceptions during initialization cannot be ignored.

like image 28
puetzk Avatar answered Oct 21 '22 17:10

puetzk