Click here to Skip to main content
15,880,427 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Thanks in advance. I am working on C++ project in DevC++ environment.
What I have observed is that in case of diamond-shape inheritance, upcasting to base class and then downcasting to derived class back is not possible?
Could you please tell me why is this so?

Following is the code which I am running.

C#
class DiamondBase
{
      public:
             int DiamondBaseData1;
};

// virtual base class will share base-class-subobject with DiamondDerived2 class
class DiamondDerived1: virtual public DiamondBase
{
      public:
             int DiamondBaseData2;

             void DiamondDerived1Fun(void){};

};

// virtual base class will share base-class-subobject with DiamondDerived1 class
class DiamondDerived2: virtual public DiamondBase
{
      public:
             int DiamondBaseData3;

             void DiamondDerived2Fun(void){};
};

class VirtualBaseDerived: public DiamondDerived1, public DiamondDerived2
{
      public:
             int DiamondBaseData4;

};



C++
void Main()
{
   DiamondDerived2 obj1;
   DiamondBase* pBase = &obj1;  // upcasting of DiamondDerived2

  ((DiamondDerived1*)pBase)->DiamondDerived1Fun();  // error: cannot convert from base to derived via "virtual-base"

  ((DiamondDerived2*)pBase)->DiamondDerived2Fun(); // error: cannot convert from base to derived via "virtual-base"

}
Posted
Updated 8-Feb-12 23:36pm
v3
Comments
krumia 13-Feb-12 3:36am    
If you are doing this just for fun/educational purposes, it's OK. But I assume you know that using this kind of inheritance can introduce problems really worse than this. So if you're doing a real project consider the design again. But considering the class names I guess you're doing this just for fun.

Check out
http://stackoverflow.com/questions/3747066/c-cannot-convert-from-base-a-to-derived-type-b-via-virtual-base-a[^]
Note that to use dynamic_cast, you must have 'polymorphic classes', i. e. your classes should have at least one virtual method. Try defining, say, a virtual destructor for each of your classes. Then use dynamic_cast.
 
Share this answer
 
Comments
Lakamraju Raghuram 9-Feb-12 11:13am    
Correct ... I missed to add this.
Emilio Garavaglia 10-Feb-12 2:11am    
"your classes should have at least one virtual method": that's typically done by making the root class having a virtual dtor.
Stefan_Lang 10-Feb-12 3:45am    
More to the point: every class that is derived from should have a virtual destructor, only the most derived class can afford to not have one. But then, C++ does not have a direct means to prevent inheritance from a given class, so you'll never know whether a class may not be used as a base class to another later.

That's why I suggested to add a virtual destructor to all classes.
Emilio Garavaglia 10-Feb-12 16:08pm    
Nope. Whatever virtual function remains virtual if overridden in a derived class. Dtors are not exceptions.

If the most inner base has a virtual dtor, all derived of whatever order will have one, even if you don't specify it as such.
Stefan_Lang 13-Feb-12 5:04am    
Hmm, I was convinced otherwise, but a short test program I wrote to prove you wrong just confirmed your point. I stand corrected, thanks for pointing it out.
To perform correct cast to a class with virtual base classes some
runtime information about the actual type of the object being
casted may be required.

And this can be acheived if we use dynamic_cast. On the other hand, the normal casting you are trying in static_cast which do not have a facility like this.
 
Share this answer
 
Comments
ggupta2009 9-Feb-12 7:28am    
Hi,
Even with dynamic_cast it is not working. Could you please comment on this?

I am coding like:
(dynamic_cast<diamondderived1*>(pBase))->DiamondDerived1Fun();

getting error: cannot dynamic_cast `pBase' (of type `class DiamondBase*') to type `class DiamondDerived1*' (source type is not polymorphic)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900