virtual hell

P

puzzlecracker

I am sure people are familiar with dirty diamond problem solved by
virtual inheritance. look at the code below and tell me how many
instances of the base class (in this case A) does class D receive?


class A
{
:
:



};


class B: virtual public A {

:
:



};


class C: virtual public A {

:
:



};


class D: public B, public C, virtual public A {

:
:



};


====

A small follow up: is inheritance by default public, protected or
private such class A: B{};?


what is the difference between
class A
{
:
:



};


class B: virtual public A {

:
:



};


class D:public B {

:
:



};


OR

class D: virtual public B {


:
:



};


============
Next: can you inline the virtual functions? is the behavior undefined,
nonstandard, etc?
 
V

Victor Bazarov

puzzlecracker said:
I am sure people are familiar with dirty diamond problem solved by
virtual inheritance. look at the code below and tell me how many
instances of the base class (in this case A) does class D receive?


class A
{
};

class B: virtual public A {
};

class C: virtual public A {
};

class D: public B, public C, virtual public A {
};

One. All instances of deriving from 'A' are virtual.

BTW, you really need to conserve whitespace. If we go about wasting
whitespace like you did in your post, we may run out of it sooner than
we think.
====

A small follow up: is inheritance by default public, protected or
private such class A: B{};?

Default is private for classes, public for structs.
what is the difference between
class A
{
};

class B: virtual public A {
};

class D:public B {
};

OR
AND

class D: virtual public B {
};

There is no difference. In both cases 'A' is a virtual base class and
has to be constructed in 'D'. In both cases 'B' is a direct base class
of 'D' and has to be constructed in 'D'.
============
Next: can you inline the virtual functions? is the behavior undefined,
nonstandard, etc?

I can. The behaviour is just fine.

V
 
P

puzzlecracker

Thanks so much Victor.

just a little small one:

but how is it done that that only one instance exists in the subclass
when the derivation is virtual... I just can visualize the process that
takes place behind the scene... any ideas?
 
P

puzzlecracker

Thanks so much Victor.

just a little small one:

but how is it done that that only one instance exists in the subclass
when the derivation is virtual... I just can visualize the process that
takes place behind the scene... any ideas?
 
P

puzzlecracker

back to that diamond problem: Why is it so that when derivation is
virtual from base class, only one instance exist, as in the first
example?


thx
 
V

Victor Bazarov

puzzlecracker said:
Thanks so much Victor.

just a little small one:




but how is it done that that only one instance exists in the subclass
when the derivation is virtual... I just can visualize the process that
takes place behind the scene... any ideas?

I am not sure what kind of help I could offer. The compiler knows all the
base classes, the entire hierarchy, when it compiles the 'D' class. It
has to come up with a layout of the class. It merges all instances of any
class inherited virtually into one, and all those that are not inherited
virtually, get left where they are. If you had left at least one of the
'A' derivations non-virtual, you'd have another instance of 'A' hanging
around in the 'D' object.

V
 
P

puzzlecracker

WOw, thx.


now real basic stuff. so when you inherit from the class...subclass
phsycially has all of its protected and public methods just as he has
is own? But why then it hides overloaded methods of base class?
 
V

Victor Bazarov

puzzlecracker said:
back to that diamond problem: Why is it so that when derivation is
virtual from base class, only one instance exist, as in the first
example?

Because the language definition says so.
 
V

Victor Bazarov

puzzlecracker said:
now real basic stuff. so when you inherit from the class...subclass
phsycially has all of its protected and public methods just as he has
is own? But why then it hides overloaded methods of base class?

Because the language definition says so.
 
M

Mike Wahler

Victor Bazarov said:
BTW, you really need to conserve whitespace. If we go about wasting
whitespace like you did in your post, we may run out of it sooner than
we think.

Lots of snow at my place. You're welcome to some or all
of it if you run out of whitespace and need a substitute.
(but you must pay for shipping).

:)

-Mike
 
P

puzzlecracker

Now it is off the topic.... I read 3 or C++ books, such as C++ primer,
C++ design revealed, and standard template library... what is your
prime choice of C++ books? Want to see if that would shift my paradigm
towards better practices.

Thanks... I know, I Know it is way off the topic.. but in the need of
your opinion, guidance.

Many thanks
 
P

puzzlecracker

Now it is off the topic.... I read 3 or C++ books, such as C++ primer,
C++ design revealed, and standard template library... what is your
prime choice of C++ books? Want to see if that would shift my paradigm
towards better practices.

Thanks... I know, I Know it is way off the topic.. but in the need of
your opinion, guidance.

Many thanks
 
M

Mike Wahler

puzzlecracker said:
Now it is off the topic.... I read 3 or C++ books, such as C++ primer,
C++ design revealed, and standard template library... what is your
prime choice of C++ books? Want to see if that would shift my paradigm
towards better practices.

Thanks... I know, I Know it is way off the topic.. but in the need of
your opinion, guidance.

IMO asking about C++ learning materials is topical here.

Have you consulted the FAQs, and the book reviews
at www.accu.org ?

C++ FAQ:
http://www.parashift.com/c++-faq-lite/

FAQ for newsgroup alt.comp.lang.learn.c-c++:
http://www.comeaucomputing.com/learn/faq/

Do you have the 'bible', Stroustrup's "The C++ Programming Language"?

-Mike
 
V

Victor Bazarov

puzzlecracker said:
Now it is off the topic....

It's not, actually...
I read 3 or C++ books, such as C++ primer,
C++ design revealed, and standard template library... what is your
prime choice of C++ books? Want to see if that would shift my paradigm
towards better practices.

Nowadays I read different books than when I was just starting with C++.
And different people respond differently to the way C++ is presented
in different books, so, I'll recommend to go to a book store and read
a few pages from the beginnings of several books. Make sure you read
the preface and the foreword. There the authors often say what the
intended readership is. The main purpose of that exercise is to see if
you're comfortable with the way the author presents the material and
with the material itself.

Of course, there are books that are highly recommended, and Mike already
gave you the link to the ACCU site. Follow it. You don't have to buy
and read all book that are marked "highly recommended", but at least you
will get an idea (or print out a list) of what or whom to prefer/avoid
when you actually get in the store.

Also, books have come up frequently here over the years, so do check out
the newsgroup archives for that.

Also, do not limit yourself to reading only C++ books. The application
domain is usually more important to be fluent in than the language in
which you implement the application. It never hurts to be fluent in the
language, but don't make it the only goal. Let it become a side effect
of using the language. Use caution when reading C++ examples and what-
not in non-C++ books. Some authors tend to pay less attention to the
language than to the application domain (and that's the RIGHT THING(tm))
just try not to pick up their bad habits.

Good luck!

V

P.S. Hey, I managed not to name a single book here...
 
M

Mike Wahler

Victor Bazarov said:
Do you think your substitute idea is gonna hold water?

In some climates, and for some applications, such as control
programs for refrigeration systems.

I also have a small quantity of yellow snow available for a
nominal additional fee.

:)

-Mike
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

Mike said:
Lots of snow at my place. You're welcome to some or all
of it if you run out of whitespace and need a substitute.
(but you must pay for shipping).

And when Stroustrup's proposal for witespace overloading will be included in
the standard, It will work for snow or we must to also overload snow?
 
M

Mike Wahler

Julián Albo said:
And when Stroustrup's proposal for witespace overloading will be included in
the standard, It will work for snow
or we must to also overload snow?

I don't think such overloading would be practical,
as it would most likely produce an avalanche of bugs.

-Mike
 

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

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,108
Latest member
AlbertEste
Top