Connecting Tech Pros Worldwide Forums | Help | Site Map

visual inheritance

anat
Guest
 
Posts: n/a
#1: Jul 19 '05
the following is a theoretical question, this code does not compile in
visual c++ so
my question is, considering c++ virtual inheritance mechanism how many
copies of V is D suppose to have?

class V{virtual f()};
class B1:public virtual V{...};
class B2:public virtual V{...};
class C:public B2, public virtual V{...};
class D: public virtual B1,public virtual B2,public virtual C{...},public
V{...};

thank you
Liz



White Wolf
Guest
 
Posts: n/a
#2: Jul 19 '05

re: visual inheritance


anat wrote:[color=blue]
> the following is a theoretical question, this code does not compile in
> visual c++ so
> my question is, considering c++ virtual inheritance mechanism how
> many copies of V is D suppose to have?[/color]

One. That is the whole idea.

--
WW aka Attila


Pete Becker
Guest
 
Posts: n/a
#3: Jul 19 '05

re: visual inheritance


anat wrote:[color=blue]
>
> the following is a theoretical question, this code does not compile in
> visual c++ so
> my question is, considering c++ virtual inheritance mechanism how many
> copies of V is D suppose to have?
>
> class V{virtual f()};
> class B1:public virtual V{...};
> class B2:public virtual V{...};
> class C:public B2, public virtual V{...};
> class D: public virtual B1,public virtual B2,public virtual C{...},public
> V{...};
>[/color]

Two. One virtual (inherited from the various bases) and one non-virtual.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
White Wolf
Guest
 
Posts: n/a
#4: Jul 19 '05

re: visual inheritance


Pete Becker wrote:[color=blue]
> anat wrote:[color=green]
>>
>> the following is a theoretical question, this code does not compile
>> in visual c++ so
>> my question is, considering c++ virtual inheritance mechanism how
>> many copies of V is D suppose to have?
>>
>> class V{virtual f()};
>> class B1:public virtual V{...};
>> class B2:public virtual V{...};
>> class C:public B2, public virtual V{...};
>> class D: public virtual B1,public virtual B2,public virtual
>> C{...},public V{...};
>>[/color]
>
> Two. One virtual (inherited from the various bases) and one
> non-virtual.[/color]

Ahh! I have missed that public V. There is a {} before it, that made me
think this is the end of the declaration of the base classes. And I am not
mistaking it will do that to a compiler, too. :-)

--
WW aka Attila


Percy
Guest
 
Posts: n/a
#5: Jul 19 '05

re: visual inheritance


On Mon, 15 Sep 2003 00:36:52 +0200 in comp.lang.c++ :

I took the liberty to change your example to below:

class V{
public:
V() { std::cout << "V\n"; }//virtual int f();
};
class B1:public virtual V
{
public:
B1() { std::cout << "B1\n"; }

};
class B2:public virtual V
{
public:
B2() { std::cout << "B2\n"; }
};
class C:public B2, public virtual V
{
public:
C() { std::cout << "C\n"; }
};
class D:public virtual B1,public virtual B2,
public virtual C,public V
{
public:
D() { std::cout << "D\n"; }
};


int main()
{
D d;
return 0;
}

compiler results:
warning: direct base `B2' inaccessible in `D'
due to ambiguity
warning: direct base `V' inaccessible in `D'
due to ambiguity
output:

V
B1
B2
B2
C
V
D


Closed Thread