views:

358

answers:

4
#include<iostream>
using namespace std;

class A

{
   int a;
   int b;
   public:
   void eat()
   {
      cout<<"A::eat()"<<endl;
   }
};

class B: public A
{
   public:
   void eat()
   {

      cout<<"B::eat()"<<endl;

   }

};

class C: public A
{

   public:
   void eat()

   {

      cout<<"C::eat()"<<endl;

   }

};

class D: public B, C
{

};

int foo(A *ptr)
{

ptr->eat();

}
main()
{

D obj;
foo(&(obj.B)); //error. How do i call with D's B part.

}

The above foo call is a compile time error. I want to call foo with obj's B part without using virtual inheritance. How do i do that.

Also, in case of virtual inheritance, why the offset information need to be stored in the vtable. This can be determined at the compile time itself. In the above case, if we pass foo with D's object, at compile time only we can calculate the offset of D's A part.

+3  A: 

Use a cast - static_cast is required here to cast up the heirarchy.

main()
{
  D obj;
  foo(static_cast<B*>(&obj));
}
1800 INFORMATION
+1  A: 

First of all, obj does not have a member named B. It Inherits from B, which means that it inherits all of B's members as its own.

You can call:

foo(static_cast<B*>(&obj));
to make it work.

e.James
+6  A: 

Inheriting twice

With double inheritance you have an ambiguity - the compiler cannot know which of the two A bases do you want to use. If you want to have two A bases (sometimes you may want to do this), you may select between them by casting to B or C. The most appropriate from default casts here is the static_cast (as the weakest available), however it is not realy needed (it is still stronger than your case needs), as you are not casting to a derived type. A custom safe_cast template should do the job:

/// cast using implicit conversions only
template <class To,class From>
inline To safe_cast( const From &from ) {return from;}

main()
{

  D obj;
  foo(safe_cast<B *>(&obj)); //error. How do i call with D's B part.

}

Compile time types - use templates

Also, in case of virtual inheritance, why the offset information need to be stored in the vtable. This can be determined at the compile time itself. In the above case, if we pass foo with D's object, at compile time only we can calculate the offset of D's A part.

This is a misconception. The foo function as it is written now has no compile type information about ptr type other than it is A *, even if you pass B * or C*. If you want foo to be able to act based on the type passed compile time, you need to use templates:

template <class TypeDerivedFromA>
int foo(TypeDerivedFromA *ptr)
{
  ptr->eat();
}

Virtual Inheritance

Your questions mentions virtual inheritance. If you want to use virtual inheritance, you need to specify so:

class B: public virtual A ...

class C: public virtual A ...

With this the code would compile, but with this solution there is no way you could select between B::A or C::A (there is only one A), therefore this is probably not what you are about.

Virtual functions

Furthermore, your questions seems to be confusing two different concepts, virtual inheritance (which means sharing one base class between two intermediate base classes) and virtual functions (which mean allowing derived class function to be called via base class pointer). If you want the B::eat to be called using A pointer, you can do this without virtual inheritance (actually virtual inheritance would prevent you doing so, as explained above), using virtual functions:

class A
{
   int a;
   int b;

   public:
   virtual void eat()
   {
      cout<<"A::eat()"<<endl;
   }
};

If virtual functions are not acceptable for you, the compile time mechanism for this are templates, as explained above.

Suma
Suma, vtable for virtual functions and virtual inheritance same? If not, what does virtual inheritance table contain.
chappar
When using virtual inheritance, the virtual table layout (and mechanism for calling virtual functions) is somewhat more complicated. See http://en.wikipedia.org/wiki/Virtual_inheritance - but be prepared, this is an advanced topic and understanding it requires effort and patience.
Suma
+1  A: 

I don't think the static_cast will work.

When you are on the foo function, all the compiler knows is that you have a pointer to A, whatever the type you passed as parameter.

If you don't use virtual inheritance, then I think there is no way to call a B function from a pointer to A.

Guishu