virtual funcs basics

P

puzzlecracker

WHat is the difference between?
Could someone explain how it's implemented, what goes on behind the
scenes in latter case?
thanks..


a)
class base {
public:

base();
~base();
void virtual func_1();
:
:
void virtual func_n();

};
class derived:public base{
public:
derived();
~derived();
void virtual func_1()
:
:
void virtual func_n();


};

vs.
b)

class base {
public:

base();
~base();
void virtual func_1();
:
:
void virtual func_n();

};
class derived: virtual public base{
public:
derived();
~derived();
void virtual func_1()
:
:
void virtual func_n();


};
 
V

Victor Bazarov

puzzlecracker said:
WHat is the difference between?
None.

Could someone explain how it's implemented, what goes on behind the
scenes in latter case?

Nothing special. Virtual inheritance is only important in the case of
multiple inheritance down the road and it has really nothing to do with
virtual functions (except that the same keyword is used in both).
thanks..
[..]
 
P

puzzlecracker

OK, I actually [implicitly ]meant the case of multiple inheritance,
particularly case of 'dirty diamond'. How does the virtuality keeps
only one copy of the base ? Can you give low and high level overview,
any relevent examples... Thanks a bunch



Victor said:
puzzlecracker said:
WHat is the difference between?
None.

Could someone explain how it's implemented, what goes on behind the
scenes in latter case?

Nothing special. Virtual inheritance is only important in the case of
multiple inheritance down the road and it has really nothing to do with
virtual functions (except that the same keyword is used in both).
thanks..
[..]
 
V

Victor Bazarov

puzzlecracker said:
OK, I actually [implicitly ]meant the case of multiple inheritance,
particularly case of 'dirty diamond'. How does the virtuality keeps
only one copy of the base ? Can you give low and high level overview,
any relevent examples... Thanks a bunch

(a) Please don't top-post.

(b) I am not sure what's low and high level overview, to be honest with
you. If the compiler encounters a virtual base class in a hierarchy of
a particular UDT, it generates a layout where a subobject of that base
class is represented only once. How it does that is not really defined
in the language. If you need to learn how your compiler does that, you
can always cast the most derived class pointer to the virtual base class
and see the difference in the pointer values, which should give you some
idea on where in the derived class object the virtual base resides. IOW,
experiment, and you shall figure out the low level stuff.

(c) If the same class is inherited from both virtually and non-virtually
in the same hierarchy, only the virtual part of it is merged into a single
instance:

struct A {}; struct B : virtual A {}; struct C : A {};
struct D : virtual A {};
struct X : B, C, D {};

Here, every 'X' will have two instances of 'A': C::A and the virtual one,
not one and not three.

Victor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top