Re: Multiple inheritance
On Mar 23, 12:04 pm, "Alf P. Steinbach" <al...@start.no> wrote:
* T Ryi:
Hi,
In the paper by stroustruphttp://www-plan.cs.colorado.edu/diwan/class-p=
apers/mi.pdf
, in page 14
the author describes ,
A virtual base is always constructed (once only) by its =91most derived=
'
class. For example:
class V { V(); V(int); ... };
class A : virtual V { A(); A(int); ... };
class B : virtual V { B(); B(int); ... };
class C : A, B { C(); C(int); ... };
V v(1); // use V(int)
A a(2); // use V(int)
B b(3); // use V()
C c(4); // use V()
Why does A ue V(int) but B and C use V() ? I don't get this part
clearly. Can someone please explain it to me ?
It's a set of typos. As stated the example is underspecified. Which const=
ructor
is invoked depends on the base class initialization specified in A and B,=
and
that's not provided above.
Moreover, with modern C++ the above uses private inheritance, which would=
make
it a bit difficult to specify the V initialization down in class C. And w=
orse,
with modern rules the A constructor doesn't have access to the private V
constructor, and so it can't do what's needed to make the comment about A
correct. So the example is not only inconsistent with modern rules, it's
self-contradictory, which means that it's not just about the article bein=
g old,
it's a set of typos.
An example where the /comments/ become correct:
struct V { V(); V( int ); };
struct A: virtual V { A( int x ): V( x ) {} };
struct B: virtual V { B( int ) {} };
struct C: A, B { C() {} C( int x ): A( x ), B( x ) {} };
V v( 1 ); // Use V( int )
A a( 2 ); // Use V( int )
B b( 3 ); // Use V()
C c( 4 ); // Use V() (because V init isn't specified by C)
Cheers & hth.,
- Alf
PS: Have you checked if there is some errate list for that article?- Hide=
quoted text -
I tried searching now for errata but didn't find anything.
I've another question
if i have
A
/ \
B C
\ /
D
/ \
E F
\ /
G
B ,C ,E ,F having virtual for their corresponding parents, will G have
single copy of A or dual . My answer is single because there will
single copy of D that will have 1 copy of A. But i want to be
correct ?
- Show quoted text -