473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Diamonds and redefinitions

I don't understand one thing about the diamond multiple inheritance. See
the code:

------------------------------------------------------

class A
{
public:
void f() {std::cout("A");}
};

class B : virtual public A
{
public:
void f() {std::cout("B");}
};

class C : virtual public A
{
};

class D : public B, public C
{
};
int main (.........) {
D d;
d.f();

........
}

------------------------------------------------------

This code compiles OK and the B::f() is called.
BUT if I remove the virtual from the following lines:
class B : virtual public A
class C : virtual public A
THEN it gives compile error:

d:\Data\VC++7.1_projects\Prova1\Prova1.cpp(73) : error C2385: ambiguous
access of 'f' in 'D'
could be the 'f' in base 'B::f'
or the 'f' in base 'A::f'
d:\Data\VC++7.1_projects\Prova1\Prova1.cpp(73) : error C3861: 'f':
identifier not found, even with argument-dependent lookup
WHY this different behaviour???
Jul 22 '05 #1
1 1081
So, where is the problem???
With your code you have this:

A
/ \
B C
\ /
D

When you remove 'virtual', you have this:

A A
| |
B C
\ /
D

So, how A will be constructed? With B-path or with C-path??

Apropos: You must use "virtual void f()" in class A (to have correct upcasting)
I don't understand one thing about the diamond multiple inheritance. See
the code:

------------------------------------------------------

class A
{
public:
void f() {std::cout("A");}
};

class B : virtual public A
{
public:
void f() {std::cout("B");}
};

class C : virtual public A
{
};

class D : public B, public C
{
};
int main (.........) {
D d;
d.f();

.......
}

------------------------------------------------------

This code compiles OK and the B::f() is called.
BUT if I remove the virtual from the following lines:
class B : virtual public A
class C : virtual public A
THEN it gives compile error:

d:\Data\VC++7.1_projects\Prova1\Prova1.cpp(73) : error C2385: ambiguous
access of 'f' in 'D'
could be the 'f' in base 'B::f'
or the 'f' in base 'A::f'
d:\Data\VC++7.1_projects\Prova1\Prova1.cpp(73) : error C3861: 'f':
identifier not found, even with argument-dependent lookup
WHY this different behaviour???

Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Tim Tyler | last post by:
I import library routines using include(...); However, importing library routines that themselves use other library routines easily leads to repeated declarations - which give errors of the form:...
2
by: dragonwebz | last post by:
hey, I'm new here but heard a lot about some new stuff I could learn on here. However I'm kinda stuck on a program thats supposed to print a diamond depend what the user inputs as a width. Yes, I...
2
by: codie | last post by:
i need to sort a list of card like the rank sample: ACE of h, ACE of d, ACE of c, ACE of s, 2, 2, 2, 2 this is my code so can anyone help me??? class Card: suitList = rankList =
3
by: aj2317 | last post by:
how do i do shapes like diamond,right angled triangle etc using just *(stars) in nested for loop???? like * ...
2
by: Immortal Nephi | last post by:
You may have heard diamond shape. You create one base class. One base class has member functions and member variables. You create two derived classes. All member functions and member variables...
7
by: MonkeyHater | last post by:
Okay, so I am have a bit of trouble with my program. The program itself will deal two hands of cards, shuffle the deck, and then deal another two hands. At the moment I have most of the code down I...
30
by: imran akhtar | last post by:
i have a balckjack code, which does not seem to run, in python, it comes up with syntax error, i have try sortng it out. does not seem to work. below is my code, if anyone can work out wht wrong...
11
by: ThaRealneSS | last post by:
Hi. I have made a blackjack code and need a some help on it. It seems to work overall but there are a few bits here and there that need sorting out, and I'm kinda stuck on it so was wondering if...
2
by: Coxmg | last post by:
I have several related tables. One table lists orders with due dates for SKUs while another table lists components of a SKU (diamonds). A third related table lists diamond IDs and diamond inventory....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.