Stefan Stefan - 1 year ago 87
C++ Question

How do I call the base class constructor?

latley I spent much programming in Java. There you call the class you Inherited from with

(you all probably know that)

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

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

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

Answer Source

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

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

Base-class constructors that take arguments have to be called there before any members are initialized.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download