Jump to content

  • Log In with Google      Sign In   
  • Create Account


C++ inheritance vs C# inheritance


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Paragon123   Members   -  Reputation: 398

Like
0Likes
Like

Posted 03 October 2013 - 12:40 PM

I just finished reading the article "Wade not in unknown waters: Part 4"

 

It mentions that multiple inheritance can be quite messy, etc.

In the article, the example shows a object that inherits from two objects that inherit from a common base object... it seems that this is the source of the issues he is addressing. The question is do the same issues arise if an object inherits from two objects that do not share an object in their own inheritance trees. I.E

Class C inherits from B1 and B2, B1 inherits from B1_1 and B1_2, and B2 inherits from B2_1 and B2_2... does this still pose an issue?

 

The second question. In C# you can not use multiple inheritance, and object can only have a single base object, however they can implement any number of Interfaces... however the concept of a interface is not explicitly defined in C++. 

The article mentions the concept of "Mix-ins", defining a mix-in as "[A] class doesn't contain any data. All its functions are usually pure virtual." which, if you remove usually, pretty well defines a C# interface. So he is saying that emulating interfaces via classes containing nothing but pure virtual functions will not cause an issue?

 

Also, he keeps mentioning virtual inheritance as the problem, but doesn't it seem more that multiple inheritance is the issue rather than virtual?



Sponsor:

#2 King Mir   Members   -  Reputation: 1910

Like
2Likes
Like

Posted 03 October 2013 - 10:54 PM

I just finished reading the article "Wade not in unknown waters: Part 4"
 
It mentions that multiple inheritance can be quite messy, etc.
In the article, the example shows a object that inherits from two objects that inherit from a common base object... it seems that this is the source of the issues he is addressing. The question is do the same issues arise if an object inherits from two objects that do not share an object in their own inheritance trees. I.E
Class C inherits from B1 and B2, B1 inherits from B1_1 and B1_2, and B2 inherits from B2_1 and B2_2... does this still pose an issue?

No that's relatively strait forward to implement; it's just nested multiple inheritance. The only strange part to this is that cast to a pointer of the parent type may not be a no-op. But that has nothing to do with nesting.
 

The second question. In C# you can not use multiple inheritance, and object can only have a single base object, however they can implement any number of Interfaces... however the concept of a interface is not explicitly defined in C++. 
The article mentions the concept of "Mix-ins", defining a mix-in as "[A] class doesn't contain any data. All its functions are usually pure virtual." which, if you remove usually, pretty well defines a C# interface. So he is saying that emulating interfaces via classes containing nothing but pure virtual functions will not cause an issue?[/size]

That's correct; a class with no data and only pure virtual methods functions just like a C# interface.
 

Also, he keeps mentioning virtual inheritance as the problem, but doesn't it seem more that multiple inheritance is the issue rather than virtual?[/size]

There's no problem, either way. There's just complexity that you have to be aware of when designing an inheritance hierarchy. C++ is a powerful tool, but that means more options, which means more complexity.

#3 Hodgman   Moderators   -  Reputation: 27841

Like
1Likes
Like

Posted 04 October 2013 - 12:36 AM

In C++ as well as regular inheritance (foo : public bar), there is virtual inheritance (foo : public virtual bar).

Virtual inheritance is the equivalent of implementing an interface in C#/Java.

Interfaces / virtual-inheritance "solves" the multiple inheritance problem.
C#/Java force you to use this solution in order to use multiple inheritance, but in C++, you can choose to use this solution, or you can choose for duplicate base classes to actually be duplicated.

#4 QuinnDexter   Members   -  Reputation: 153

Like
0Likes
Like

Posted 04 October 2013 - 09:49 AM

I've been in MI hell with C++ on many occasions and when you are put on a project and encounter a class with three or four base classes I can feel my testes retreat. 

 

There's a good blog here talking about why C# doesn't support multiple inheritance which might answer your initial question.

 

http://blogs.msdn.com/b/csharpfaq/archive/2004/03/07/85562.aspx



#5 Bregma   Crossbones+   -  Reputation: 4769

Like
0Likes
Like

Posted 04 October 2013 - 12:15 PM

As a general statement, any time you run into most MI problems in C++, it's because inheritance was the wrong tool for the job in the first place.


Stephen M. Webb
Professional Free Software Developer

#6 SiCrane   Moderators   -  Reputation: 9408

Like
3Likes
Like

Posted 04 October 2013 - 03:56 PM


Virtual inheritance is the equivalent of implementing an interface in C#/Java.

There are a few differences between C++ virtual inheritance and implementing an interface in C#. One is how name resolution is handled in base classes vs. interfaces. In C++ if you inherit from an interface class that has a pure virtual function foo() and from a base class that has a foo() implementation, those are considered to be completely separate. In order for the base class foo() to be used for the interface's foo() you need to manually forward the function call. However, in C# if you have a base class that has a foo() implementation, it's automatically used as an implementation by any interfaces. You can also virtually inherit from classes with implementations and there are some interesting rules about inheriting from multiple base classes that virtually inherit from from the same base class that get fairly complicated (especially in C++11, or rather the rules are the same but C++11 got a lot more explicitly mathematical about how they are meant to be implemented).






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS