Skip to main content
gamedev.net
🔒 Locked

virtual inheritance

Started by SkatMan Oct 19, 2000 at 3:45 AM 8 replies 400+ views
Original Post
SkatMan
SkatMan
Hello all. I want to describe a situation in C++ and you tell me what to do: I have: class a; class b : virutal public a; class c : virtual public a; class d : public b, public c; Ok, that''s cool ''till now, but what if I want to create ''class e'' that will inherit both b and c, but won''t let them share class a''s variables. Must I redefine new classes for b and c which will not inherirt a virtually? I don''t like that. Oren.
Danack
Danack

Why don''t you like that ?

I thought the only effect of the virtual keyword was to make them share the variables....


dan


Game production:
Good, quick, cheap: Choose two.
Game production:Good, quick, cheap: Choose two.
microdot
microdot
What your asking is a little vague. What do you mean by ''share''. If what your looking for is two sets of variables from class a, then perhaps you should have class e make class b and class c member variables instead of using inheritance. Now if it''s a matter of variable visibility, then you should look into private and protected base class access. Need more info to be more specific.
<span class="smallfont">That is not dead which can eternal lieAnd with strange aeons even death may die.   -- "The Nameless City" - H. P. Lovecraft</span>
SkatMan
SkatMan
By share i mean only one instance of the variable will exist.

Well.. I don''t think it would be a good idea to make class e contain class b and class c because he is of a ''b and c type''.
Should be passed as an argument for a function that get class b, etc.

It''s not a matter of visibility.


Oren.
microdot
microdot
Well, is class a ever passed as an argument. If not perhaps you could have class a be member data as necessary. Another question. Of all these classes, which ones actually are instantiated. Perhaps rearranging members, and making some of the classes purely abstract is in order.
<span class="smallfont">That is not dead which can eternal lieAnd with strange aeons even death may die.   -- "The Nameless City" - H. P. Lovecraft</span>
microdot
microdot
Ok, so let me get this straight. You want class D and class E to be able to be pointed to by a class A, class B or class C pointer, but you want class E to have two sets of class A''s variables (one through class B and the other through class C), and class D to have only one set of class A''s variables. Is this right. If so the only thing I can think of, without having to completely restucture your class heirarchy, is in the case of class D to have two sets of class A''s variables, instead of one, and have the two update one another if there is a change. Not very memory efficient, but I can''t think of anything off the top of my head, outside of changing how everything works. This is a very difficult problem without a specific case to apply the problem to.
<span class="smallfont">That is not dead which can eternal lieAnd with strange aeons even death may die.   -- "The Nameless City" - H. P. Lovecraft</span>
JonStelly
JonStelly
Something that tends to get people new or at least not as experienced in OOP is the desire to have unrelated classes or classes that serve completely different functions on the same level in the hierarchy.

I''m assuming that class e is similiar but not LIKE d. You might need an extra layer between B,C and E to clear things up, or perhaps you have some members of classes A,B,C that should be elsewhere. It might help if you give us class names so we can get a very basic idea of what you''re doing.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.