Reputation: 24911
I've been reading about inheritance but I still dont have it 100% clear, here goes my question: I have for example a class full of stuff:
class Something
{
public:
Something();
Something(int A);
Something(Something S);
~Something();
Something& operator=(const Something & s);
Something operator+(const Something & s) const;
// more methods ...
protected:
int A,B,C;
}
All methods implemented somewhere, and now I just want a new class to add one more attribute and maybe one method, but I still want the same behavior as the SuperClass (constructor, destructor, methods, operators), so I do something like this:
class SomethingMore : public Something
{
public:
void OnlyMethodHere();
private:
int D;
}
But I get something like:
undefined reference to SomethingMore::SomethingMore(int)
Is there a ay to tell the compiler to use everything just like it is in the SuperClass or do I have to make a SuperClass::method() in every child's method?
Upvotes: 1
Views: 448
Reputation: 20730
You can obtain the same effect as in this answer, in C++0x with a sort of catch-all constructor:
class SomethingMore
{
public:
.......
template<class... Args>
SomethingMore(Args&&... args) : Something(std::forward<Args>(args...)) {}
}
Upvotes: 0
Reputation: 6181
In c++0x it is possible to do by:
class BaseClass
{
public:
BaseClass(int iValue);
};
class DerivedClass : public BaseClass
{
public:
using default BaseClass;
};
, but i don't know of any compiler that supports it by now. If you want to keep compatible with older compilers you have to repeat all constructors in derived class like
class BaseClass
{
public:
BaseClass(int iValue);
};
class : public BaseClass
{
public:
DerivedClass(int iValue):BaseClass(iValue){}
};
Upvotes: 0
Reputation: 27674
You don't have to repeat for methods. but for constructor, you have to define them properly in the sub class. Anyway the code is boiler plate unless you have anything extra.
SomethingMore::SomethingMore(int i_):Something(i_)
{
}
Upvotes: 4