views:

1116

answers:

3
+2  A: 

You have to explicitly define the constructor in B and explicitly call the constructor for the parent.

B(int x) : A(x) { }

or

B() : A(5) { }

grepsedawk
+12  A: 

Constructors are not inherited. They are called implicitly or explicitly by the child constructor.

The compiler creates a default constructor (one with no arguments) and a default copy constructor (one with an argument which is a reference to the same type). But if you want a constructor that will accept an int, you have to define it explicitly.

class A
{
public: 
    explicit A(int x) {}
};

class B: public A
{
public:
    explicit B(int x) : A(x) { }
};
Avi
A: 

In current C++ constructors cannot be inherited and you need to inherit them manually one by one by calling base implementation on your own. Note however that upcoming C++0x standard should allow constructor inheritance. For more see Wikipedia C++0x article. With the new standard you should be able to write:

class A
{
    public: 
        explicit A(int x) {}
};

class B: public A
{
     using A::A;
};
Suma