0

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 name;}

    protected:
        std::string name;
};

class B : public virtual A {};
class C : public virtual A {};

class D : public B, public C {};
blubberbernd
  • 3,641
  • 8
  • 35
  • 46

1 Answers1

2

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++

Community
  • 1
  • 1
Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • No, you don't have to explicitly initialize the virtual base if it is default-constructible, which in this case it is. No? – Armen Tsirunyan Mar 19 '11 at 09:15
  • Wouldn't the compiler generate a working default constructor since there is no constructor defined in the code from the question? – rve Mar 19 '11 at 09:18
  • @Armen: You're right. I missed that point. Updated my answer! – Nawaz Mar 19 '11 at 09:20