 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
puzzlecracker Guest
|
Posted: Sat Jan 15, 2005 4:03 am Post subject: VIRTUAL PROBLEMS |
|
|
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?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
L.Suresh Guest
|
Posted: Sat Jan 15, 2005 10:24 am Post subject: Re: VIRTUAL PROBLEMS |
|
|
| Quote: | class A {};
class B: virtual public A {};
class C: virtual public A {};
class D: public B, public C, virtual public A {};
|
D has one subobject A.
| Quote: | A small follow up: is inheritance by default public, protected or
private such class A: B{};?
|
11.2
"In the absence of an access-specifier for a base class, public is
assumed when the derived class is
declared struct and private is assumed when the class is declared
class."
| Quote: | what is the difference between
class A {};
class B: virtual public A {};
class D:public B {};
OR
class D: virtual public B {};
|
The layout for D in both cases should look the same. Both have one B
subobject.
| Quote: | Next: can you inline the virtual functions? is the behavior
undefined,
nonstandard, etc?
|
Yes, but mostly it wont buy you anything. Inline is compile-time, while
virtual
is runtime. Usually the inline will be ignored. But the compiler can
inline if
it knows the exact type of the object at compile time, when you do not
use
pointers/references.
--lsu
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
carletoncourses@yahoo.ca Guest
|
Posted: Sun Jan 16, 2005 4:27 am Post subject: Re: VIRTUAL PROBLEMS |
|
|
Hello,
Please see inlines.
Bill
puzzlecracker wrote:
| Quote: | 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 {
};
|
I believe one object of D would receive one subobject each of A,
B and C.
| Quote: |
====
A small follow up: is inheritance by default public, protected or
private such class A: B{};?
|
Let me answer this question in an indirect way:
Say we have this program,
#include <iostream>
class A {
public:
inline virtual void f1() { std::cout << "Hello" << std::endl; }
};
class B: A {
};
int main(int argc, char * argv[] )
{
B b;
b.f1();
return 0;
}
If we try to compile it using Visual C++ 6.0, we would get an error
similar to this:
error C2248: 'f1' : cannot access public member declared in class 'A'
The error goes away if we modify B's definition as
class B: public A {
};
| Quote: | what is the difference between
class A
{
};
class B: virtual public A {
};
class D:public B {
};
OR
class D: virtual public B {
};
|
Assuming that you are trying to distinguish between when
to use "public" versus "virtual public", let me give you
an example:
Say we have the following class definitions:
class A {
};
class B: virtual public A {
};
class C: virtual public A {
};
class D: public B, public C {
};
In this case, an object of D will have a subobject of A,
B and C. I believe, this is usually called "virtual inheritance".
But, if definitions of B and C is modified as,
class B: public A {
};
class C: public A {
};
then, an object of D will have two subobjects of A (one from B
and one from C), one subobject of B and one subobject of C.
Virtual inheritance is benefitial in terms of size of object but of
course
at the cost of processing overhead.
| Quote: |
============
Next: can you inline the virtual functions? is the behavior
undefined,
nonstandard, etc?
|
Although a compiler like VC++ 6.0 won't complain if you use the
phrase "inline virtual" in front of a function definition,
however, I believe the compiler will "actually" not inline that
function.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
carletoncourses@yahoo.ca Guest
|
Posted: Sun Jan 16, 2005 4:59 am Post subject: Re: VIRTUAL PROBLEMS |
|
|
puzzlecracker wrote:
| Quote: | 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{};?
|
I would like to add a little to what L.Suresh said in the last posting.
Usually the phrase "virtual public" is used in multiple inheritance
scenerios. In the situation above, the usage may not be buy you much.
But it would be useful in the scenerio similar to the first question:
class A {};
class B: virtual public A {};
class C: virtual public A {};
class D: public B, public C {};
In this case, D will have one subobject each of A, B and C.
But if the definitions of B and C are changed to
class B: public A {};
class C: public A {};
then D will have two subobjects of A (one from B and the other from C)
and one subobject each of B and C.
Bill
| Quote: |
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?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
David Olsen Guest
|
Posted: Sat Jan 22, 2005 11:29 am Post subject: Re: VIRTUAL PROBLEMS |
|
|
L.Suresh wrote:
| Quote: | what is the difference between
class A {};
class B: virtual public A {};
class D:public B {};
OR
class D: virtual public B {};
The layout for D in both cases should look the same. Both have one B
subobject.
|
Both versions of D will indeed have one B subobject, but the layouts
will almost definitely be different. Virtual bases are usually layed
out differently than non-virtual bases, though the details will depend
on the particular implementation.
--
David Olsen
[email]qg4h9ykc5m (AT) yahoo (DOT) com[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|