|
-
March 15th, 2009, 01:35 PM
#1
Multiple inheritance and virtual functions.
Hello everyone.
I've recently been working with OO in C++ after switching from about a year of programming exclusively in Java.
Since I have no experience with multiple inheritance I've been Googling trying to find an answer to my question but can't find one.
Suppose I have classes A, B, C, D, and E.
The hierarchy looks as follows:
Code:
A
|
|-----
| |
B C
| |
| |
D |
| |
|---- |
|
E
This is, B and C inherit from A, and D inherits from B, and E inherits from D and C.
I understand this is a typical example of the "diamond" problem and that the best way to fix the ambiguity is through defining the parents of the subclass as 'virtual'.
However, in this case, do I want to define only D and C as virtual, or D B and C as virtual, or just B and C as virtual?
Thanks!
-
March 15th, 2009, 06:30 PM
#2
Re: Multiple inheritance and virtual functions.
I found lots of results when I googled. This was third in the search.
-
March 15th, 2009, 10:56 PM
#3
Re: Multiple inheritance and virtual functions.
B and C should be virtual.
Thanks for your help.
-
March 16th, 2009, 02:47 AM
#4
Re: Multiple inheritance and virtual functions.
It's good to know what to do, but in general I would avoid such diamond hierarchies.
-
March 16th, 2009, 03:01 AM
#5
Re: Multiple inheritance and virtual functions.
I think defining B and C as virtual would be a much better choice but this choice can differ depending on the structure and how u intend to use the varous classes .
Keep in mind only one thing that if logic or features are going to change would your current declaraton will be able to adopt the change without having any effect on the control that you currently have on your code
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|