I have a class A with virtual inline getters and setters. From A there are two classes B and C derived. And I have a class D, derived from B and C. Creatung an object from D and using the getName() results in "undefined reference to getName()". Removing "inline" doens't work. The header file is included correctly. What's the problem here?
class A
{
public:
virtual inline std::string getName() const{return开发者_StackOverflow中文版 name;}
protected:
std::string name;
};
class B : public virtual A {};
class C : public virtual A {};
class D : public B, public C {};
Your code compiles fine with/without inline
: with inline and without inline
But remember this otherwise : in a virtual inheritance, you've to initialize the base explicitly IF the base class constructor takes parameter as,
class D : public B, public C
{
public:
D(string s) : A(s), B(s), C(s){}
//^^^^ note this!
};
Just D(string s) : B(s), C(s)
would not be enough: http://ideone.com/MPUPj
A(s)
is also needed : http://ideone.com/DNLkA
See this topic for more detail: about virtual base class and virtual inheritance in C++
精彩评论