Skip to main content
GameDev.net gamedev.net
🔒 Locked

virtual class inheritance

Started by Cybrosys Apr 13, 2005 at 1:15 AM 4 replies 1.5k views
Original Post
Cybrosys
Cybrosys
Don't get the title wrong. I just saw this bit of code in the max sdk and a question mark popped up. Thoughts like, maybe it's just a compilertrim add, to better help the compiler compile the code by giving it more information. class IDX9VertexShader : virtual public IVertexShader, public BaseInterface virtual public IVertexShader Could anyone explain to me why exactly they descided to put a virtual infront of the first inheritance? Besides the fact that IVertexShader is pure/partial virtual, which can be understood by the Interface prefix (I).
JohnBolton
JohnBolton
I don't know the specifics of your case, but in general:

Without virtual inheritance, if classes B and C are both derived from A, and D is derived from B and C, then D will contain two instances of A. However, if you use virtual inheritance, then D will contain only one A.
non-virtual   virtual      D          D    / \        / \    B   C      B   C   |   |       \ /   A   A        A 

John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Eric Lengyel
Eric Lengyel
This usage of virtual doesn't have anything to do with virtual functions. What it means here is that if you create a subclass of IDX9VertexShader which is also a subclass of another class having IVertexShader as a virtual base class, then the data structure will only contain one copy of the IVertexShader class. The compiler implementation of this is somewhat messy, so I personally avoid using virtual base classes. You can find more info in the ISO C++ Standard in section 10.1, paragraphs 3-6.

-- Eric Lengyel
Cybrosys
Cybrosys
Eric, where exactly is the ISO C++ Standard published. I've tried searching msdn for it and google but i haven't been able to find it.

Thanks for the fast answer regarding virtual though, both of you. Using and not using virtual inheritance offers some nice design or features because both inheritances can store multiple data information or merge it and store one copy of it. (Using a, inherits b and c, b and c inherits d. So a could access b's d data or c's d data separately using b::d alternatively c::d)

[Edited by - Cybrosys on April 13, 2005 2:43:26 AM]
Shannon Barber
Shannon Barber
Quote:
Original post by Cybrosys
Could anyone explain to me why exactly they descided to put a virtual infront of the first inheritance? Besides the fact that IVertexShader is pure/partial virtual, which can be understood by the Interface prefix (I).


Generally, in C++ interfaces/pure-abstract-base-classes should inherit virtually since you will only want one implementation of that interface.
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Qw3r7yU10p!
Qw3r7yU10p!
Quote:
Original post by Cybrosys
Eric, where exactly is the ISO C++ Standard published. I've tried searching msdn for it and google but i haven't been able to find it.


A copy of the draft standard is here (amongst other places)

You can buy the official ANSI Standard from the ANSI website here. It's US$19 for the electronic version.

You can buy it at amazon here amongst other places in dead wood form.

Topic Locked

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

Sign in to reply to this topic.