How to call base class constructor?

latley I programmed a lot in Java. There you call the class you inherit with super(); (all of you probably know this)

Now I have a class in C ++ that has a default constructor that takes some arguments. Example:

 class BaseClass { public: BaseClass(char *name); .... 

If I inherit a class, it gives me a warning that there is no suitable default constructor. So is there something like super() in C ++, or do I need to define a function where I initialize all the variables?

+49
c ++ constructor
Aug 03 2018-11-11T00:
source share
6 answers

You do this in the initializer list of the subclass constructor.

 class Foo : public BaseClass { public: Foo() : BaseClass("asdf") {} }; 

Base class constructors that take arguments must be called there before any elements are initialized.

+62
Aug 03 '11 at 8:40
source share

In the header file, define the base class:

 class BaseClass { public: BaseClass(params); }; 

Then define the derived class as an inheriting BaseClass:

 class DerivedClass : public BaseClass { public: DerivedClass(params); }; 

In the source file, define the BaseClass constructor:

 BaseClass::BaseClass(params) { //Perform BaseClass initialization } 

By default, a derived constructor calls only the base default constructor with no parameters; therefore, in this example, the base class constructor is NOT automatically called when the derived constructor is called, but can be achieved by simply adding the syntax of the base class constructor after the colon ( : . Define a derived constructor that automatically calls its base constructor:

 DerivedClass::DerivedClass(params) : BaseClass(params) { //This occurs AFTER BaseClass(params) is called first and can //perform additional initialization for the derived class } 

The BaseClass constructor BaseClass called before the DerivedClass constructor, and the same / different params parameters can be redirected to the base class, if necessary. This can be nested for deeper derived classes. The resulting constructor should call EXACTLY ONE base constructor. AUTOMATICALLY destructors are called in REVERSE order, which was called by the constructors.

EDIT: There is an exception to this rule if you inherit from any virtual classes, usually to achieve multiple or diamond inheritance. Then you SHOULD explicitly call the base constructors of all the virtual base classes and explicitly pass parameters, otherwise they will only call their default constructors without any parameters. See virtual inheritance - constructor omissions

+19
May 12 '15 at 21:42
source share

You should use initiailzers:

 class DerivedClass : public BaseClass { public: DerivedClass() : BaseClass(<insert arguments here>) { } }; 

This is also how you create members of your class that don't have constructors (or you want to initialize them). Any members not mentioned will be initialized by default. For example:

 class DerivedClass : public BaseClass { public: DerivedClass() : BaseClass(<insert arguments here>) , nc(<insert arguments here>) //di will be default initialized. { } private: NeedsConstructor nc; CanBeDefaultInit di; }; 

The order in which the members are listed does not matter (although the constructors must be first), but the order in which they are built is in the order of declaration. So nc will always be built before di .

+17
Aug 03 '11 at 8:40
source share

Regarding the alternatives are super; in most cases, you use the base class either in the initialization list of the derived class, or using the Base::someData syntax when you do work elsewhere and the derived class overrides the data.

 struct Base { Base(char* name) { } virtual ~Base(); int d; }; struct Derived : Base { Derived() : Base("someString") { } int d; void foo() { d = Base::d; } }; 
+4
Aug 03 2018-11-11T00:
source share

Use the name of the base class in the list of initializers. The list of initializers appears after the constructor signature in front of the method body and can be used to initialize base classes and members.

 class Base { public: Base(char* name) { // ... } }; class Derived : Base { public: Derived() : Base("hello") { // ... } }; 

Or the pattern used by some people is to define β€œsuper” or β€œbase” on their own. Perhaps some people who prefer this technique are Java developers who are switching to C ++.

 class Derived : Base { public: typedef Base super; Derived() : super("hello") { // ... } }; 
+2
Aug 03 2018-11-11T00:
source share

In C ++, there is no super (). You must call the base constructor explicitly by name.

+2
Aug 03 2018-11-11T00:
source share



All Articles