Why does this code:
class A
{
public:
explicit A(int x) {}
};
class B: public A
{
};
int main(void)
{
B *b = new B(5);
delete b;
}
Result in these errors:
main.cpp: In function ‘int main()’: main.cpp:13: error: no matching function for call to ‘B::B(int)’ main.cpp:8: note: candidates are: B::B() main.cpp:8: note: B::B(const B&)
Shouldn't B inherit A's constructor?
(this is using gcc)
Correct Code is
Error is b/c Class B has not parameter constructor and second it should have base class initializer to call the constructor of Base Class parameter constructor
You have to explicitly define the constructor in B and explicitly call the constructor for the parent.
or
This is straight from Bjarne Stroustrup's page:
If you so choose, you can still shoot yourself in the foot by inheriting constructors in a derived class in which you define new member variables needing initialization:
In C++03 standard constructors cannot be inherited and you need to inherit them manually one by one by calling base implementation on your own.
If your compiler supports C++11 standard, there is a constructor inheritance. For more see Wikipedia C++11 article. With the new standard you write:
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.
UPDATE: In C++11, constructors can be inherited. See Suma's answer for details.
How about using a template function to bind all constructors?