473,594 Members | 2,756 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem with multiple inheritance

dl
I have two classes, say A and B, both having a data member 'int n';
private in A, public in B.

When I derive class C from both public A and public B, B::n should be
visible to C while A::n should not be.

But if I compile with g++-4.0.3 the following snippet:

class A {
int i;
public:
A () {}
};

class B {
public:
int i;
B () {}
};

class C : public A, public B {
public:
C () { i = 3; }
};

int main () {
C c;
return 0;
}

I get the following error:

test.cpp: In constructor 'C::C()':
test:15: error: reference to 'i' is ambiguous
test:9: error: candidates are: int B::i
test:2: error: int A::i

Nothing dramatic, of course I can change the names or use access
specifiers. I just would like to know if I am misinterpreting chapter 6
of Stroustrup 2nd edition or if this is a compiler problem.

Thanks for the attention,

Daniele

Nov 10 '06 #1
14 3586
dl wrote:
I have two classes, say A and B, both having a data member 'int n';
'int i;'
private in A, public in B.

When I derive class C from both public A and public B, B::n should be
visible to C while A::n should not be.
They are both visible. A::i, however, is *inaccessible*. The rules
of accessibility and visibility of names are orthogonal. First, the
rules of visibility are applied, then the access is *verified*. In
your case two variables that have the same name and the same type
conflict with each other. You need to tell the compiler which one
you mean to use by qualifying the name: A::i or B::i.
>
But if I compile with g++-4.0.3 the following snippet:

class A {
int i;
public:
A () {}
};

class B {
public:
int i;
B () {}
};

class C : public A, public B {
public:
C () { i = 3; }
};

int main () {
C c;
return 0;
}

I get the following error:

test.cpp: In constructor 'C::C()':
test:15: error: reference to 'i' is ambiguous
test:9: error: candidates are: int B::i
test:2: error: int A::i

Nothing dramatic, of course I can change the names or use access
specifiers.
Wrong terminology. The syntax 'B::' (or 'A::') is called "nested name
specifier". Access specifier is the "private" or "public".
I just would like to know if I am misinterpreting chapter
6 of Stroustrup 2nd edition or if this is a compiler problem.
Most likely.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 10 '06 #2
dl
Victor Bazarov ha scritto:
dl wrote:
I have two classes, say A and B, both having a data member 'int n';

'int i;'
private in A, public in B.

When I derive class C from both public A and public B, B::n should be
visible to C while A::n should not be.

They are both visible. A::i, however, is *inaccessible*. The rules
of accessibility and visibility of names are orthogonal. First, the
rules of visibility are applied, then the access is *verified*. In
your case two variables that have the same name and the same type
conflict with each other. You need to tell the compiler which one
you mean to use by qualifying the name: A::i or B::i.

But if I compile with g++-4.0.3 the following snippet:

class A {
int i;
public:
A () {}
};

class B {
public:
int i;
B () {}
};

class C : public A, public B {
public:
C () { i = 3; }
};

int main () {
C c;
return 0;
}

I get the following error:

test.cpp: In constructor 'C::C()':
test:15: error: reference to 'i' is ambiguous
test:9: error: candidates are: int B::i
test:2: error: int A::i

Nothing dramatic, of course I can change the names or use access
specifiers.

Wrong terminology. The syntax 'B::' (or 'A::') is called "nested name
specifier". Access specifier is the "private" or "public".
I just would like to know if I am misinterpreting chapter
6 of Stroustrup 2nd edition or if this is a compiler problem.

Most likely.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Thank you.

Nov 10 '06 #3
I get the following error:
>
test.cpp: In constructor 'C::C()':
test:15: error: reference to 'i' is ambiguous
test:9: error: candidates are: int B::i
test:2: error: int A::i

Thanks for the attention,

Daniele
This is not a compiler error, rather it is a constant plauge on
multiple inheretance (spelling?)... Because the object C is a child of
both A and B, it inherents variable i from both despite the fact that
it is not made public (for more information, look up the keywords
'private' and 'protected'). I would not recommend using multiple
inheritance unless you are more comforatable with the concept (it also
adds a bit of overhead to your applications). However, if you would
like tho continue, I beleive the problem here can be resolved by use of
the 'virtual' keyword. When you declare a class 'virtual' you are
stating that when inherited it objects should inherit all values unless
they already exist for some reason (more or less... mostly less). What
you should probably do is create another virtual class (lets say E)
that contains i, have A and B inherit from E and then have C inherit
from A and B. Hope that helps and isn't to vauge or confusing...
-Leif902
http://www.greenmangames.vze.com

(or if you don't want to do all that, you can just reference the
class... like 'c.A::i', this however is not a very good solution as it
forces class information to be placed in application code which is a
big no...)

Nov 10 '06 #4
dl

Leif902 ha scritto:
I get the following error:

test.cpp: In constructor 'C::C()':
test:15: error: reference to 'i' is ambiguous
test:9: error: candidates are: int B::i
test:2: error: int A::i

Thanks for the attention,

Daniele

This is not a compiler error, rather it is a constant plauge on
multiple inheretance (spelling?)... Because the object C is a child of
both A and B, it inherents variable i from both despite the fact that
it is not made public (for more information, look up the keywords
'private' and 'protected'). I would not recommend using multiple
inheritance unless you are more comforatable with the concept (it also
adds a bit of overhead to your applications). However, if you would
like tho continue, I beleive the problem here can be resolved by use of
the 'virtual' keyword. When you declare a class 'virtual' you are
stating that when inherited it objects should inherit all values unless
they already exist for some reason (more or less... mostly less). What
you should probably do is create another virtual class (lets say E)
that contains i, have A and B inherit from E and then have C inherit
from A and B. Hope that helps and isn't to vauge or confusing...
-Leif902
http://www.greenmangames.vze.com

(or if you don't want to do all that, you can just reference the
class... like 'c.A::i', this however is not a very good solution as it
forces class information to be placed in application code which is a
big no...)
Thank you, too.

I must say that IMHO this is not a very good feature of C++. Objects
should be free of having *private* variables of any name and any type,
without clash with any usage which can be done of the object, including
multiple inheritance. If I need a variable for local purposes in my
object A, once I declare it private, why should I also worry about the
fact that the same name and type could be present in a completely
different object B, maybe writted by somebody else, and that both A and
B could be inherited by C, maybe written by a third person?
Anyway if it is like that, I must comply.

Regards,

dl

Nov 10 '06 #5

dl wrote:
Leif902 ha scritto:
I get the following error:
>
test.cpp: In constructor 'C::C()':
test:15: error: reference to 'i' is ambiguous
test:9: error: candidates are: int B::i
test:2: error: int A::i
>
Thanks for the attention,
>
Daniele
This is not a compiler error, rather it is a constant plauge on
multiple inheretance (spelling?)... Because the object C is a child of
both A and B, it inherents variable i from both despite the fact that
it is not made public (for more information, look up the keywords
'private' and 'protected'). I would not recommend using multiple
inheritance unless you are more comforatable with the concept (it also
adds a bit of overhead to your applications). However, if you would
like tho continue, I beleive the problem here can be resolved by use of
the 'virtual' keyword. When you declare a class 'virtual' you are
stating that when inherited it objects should inherit all values unless
they already exist for some reason (more or less... mostly less). What
you should probably do is create another virtual class (lets say E)
that contains i, have A and B inherit from E and then have C inherit
from A and B. Hope that helps and isn't to vauge or confusing...
-Leif902
http://www.greenmangames.vze.com

(or if you don't want to do all that, you can just reference the
class... like 'c.A::i', this however is not a very good solution as it
forces class information to be placed in application code which is a
big no...)

Thank you, too.

I must say that IMHO this is not a very good feature of C++. Objects
should be free of having *private* variables of any name and any type,
without clash with any usage which can be done of the object, including
multiple inheritance. If I need a variable for local purposes in my
object A, once I declare it private, why should I also worry about the
fact that the same name and type could be present in a completely
different object B, maybe writted by somebody else, and that both A and
B could be inherited by C, maybe written by a third person?
Anyway if it is like that, I must comply.
You can reduce it to a single name using something like this:

class A {
public:
A( int first, int second, int third ) : members( first, second, third
) {}
private:
struct members_t {
members_t( int first, int second, int third ) : a( first ), b( second
), c( third ) {}
int a, b, c;
} members;
};

Depending on what you're doing you'll have to forward some
constructors.

Probably not worth the extra effort though unless you're implementing a
library and name clashes are a problem. There's also pimpl.
K

Nov 11 '06 #6
dl
Kirit Sælensminde ha scritto:
dl wrote:
Leif902 ha scritto:
I get the following error:

test.cpp: In constructor 'C::C()':
test:15: error: reference to 'i' is ambiguous
test:9: error: candidates are: int B::i
test:2: error: int A::i

Thanks for the attention,

Daniele
>
This is not a compiler error, rather it is a constant plauge on
multiple inheretance (spelling?)... Because the object C is a child of
both A and B, it inherents variable i from both despite the fact that
it is not made public (for more information, look up the keywords
'private' and 'protected'). I would not recommend using multiple
inheritance unless you are more comforatable with the concept (it also
adds a bit of overhead to your applications). However, if you would
like tho continue, I beleive the problem here can be resolved by use of
the 'virtual' keyword. When you declare a class 'virtual' you are
stating that when inherited it objects should inherit all values unless
they already exist for some reason (more or less... mostly less). What
you should probably do is create another virtual class (lets say E)
that contains i, have A and B inherit from E and then have C inherit
from A and B. Hope that helps and isn't to vauge or confusing...
-Leif902
http://www.greenmangames.vze.com
>
(or if you don't want to do all that, you can just reference the
class... like 'c.A::i', this however is not a very good solution as it
forces class information to be placed in application code which is a
big no...)
Thank you, too.

I must say that IMHO this is not a very good feature of C++. Objects
should be free of having *private* variables of any name and any type,
without clash with any usage which can be done of the object, including
multiple inheritance. If I need a variable for local purposes in my
object A, once I declare it private, why should I also worry about the
fact that the same name and type could be present in a completely
different object B, maybe writted by somebody else, and that both A and
B could be inherited by C, maybe written by a third person?
Anyway if it is like that, I must comply.

You can reduce it to a single name using something like this:

class A {
public:
A( int first, int second, int third ) : members( first, second, third
) {}
private:
struct members_t {
members_t( int first, int second, int third ) : a( first ), b( second
), c( third ) {}
int a, b, c;
} members;
};

Depending on what you're doing you'll have to forward some
constructors.

Probably not worth the extra effort though unless you're implementing a
library and name clashes are a problem. There's also pimpl.
K
Thank you Kirit.

I didn't know pimpl (looks like a bad word). Do you mean the thing
described eg in
http://www.gamedev.net/reference/art...rticle1794.asp ? Thanks for
pointing it out.

Anyway all possible solutions, including pimpl, look clumsy to me; the
point is that I see no reason to have something visible and
inaccessible, to use the words of Victor. If A::i is inaccessible, then
in fact there is no ambiguity.

Regards,

dl

Nov 11 '06 #7
dl

Leif902 ha scritto:
I beleive the problem here can be resolved by use of
the 'virtual' keyword. When you declare a class 'virtual'
If I declare the class 'virtual', g++ complains as follows:

error: 'virtual' can only be specified for functions
you are
stating that when inherited it objects should inherit all values unless
they already exist for some reason (more or less... mostly less). What
you should probably do is create another virtual class (lets say E)
that contains i, have A and B inherit from E and then have C inherit
from A and B. Hope that helps and isn't to vauge or confusing...
Regards,

dl

Nov 11 '06 #8
Leif902 wrote:
>I get the following error:

test.cpp: In constructor 'C::C()':
test:15: error: reference to 'i' is ambiguous
test:9: error: candidates are: int B::i
test:2: error: int A::i

Thanks for the attention,

Daniele

This is not a compiler error, rather it is a constant plauge on
multiple inheretance (spelling?)... Because the object C is a child of
both A and B,
C is not an object. It's a type. If you slur that distinction you'll be
constantly lost.

it inherents variable i from both despite the fact that
it is not made public (for more information, look up the keywords
'private' and 'protected'). I would not recommend using multiple
inheritance unless you are more comforatable with the concept (it also
adds a bit of overhead to your applications). However, if you would
like tho continue, I beleive the problem here can be resolved by use of
the 'virtual' keyword.
No, that won't help.

When you declare a class 'virtual' you are
stating that when inherited it objects should inherit all values unless
they already exist for some reason (more or less... mostly less).
Far less. A base class can be labeled virtual, and if there's more than
one virtual base of that type, they'll be merged. That applies to base
classes, not to random data items that happen to have the same name.
Here, the conflicting names come from two different base classes, so
virtual bases won't help.

What
you should probably do is create another virtual class (lets say E)
that contains i, have A and B inherit from E and then have C inherit
from A and B.
This assumes that it's okay for A and B to share the same copy of i.
There's nothing here to indicate that that's the case.

--

-- Pete
Roundhouse Consulting, Ltd. -- www.versatilecoding.com
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
Nov 11 '06 #9
dl wrote:
Leif902 ha scritto:
>I beleive the problem here can be resolved by use of
the 'virtual' keyword. When you declare a class 'virtual'

If I declare the class 'virtual', g++ complains as follows:

error: 'virtual' can only be specified for functions
As I said elsewhere, virtual is not the answer. It can be applied to
base classes, which is what the other poster was talking about, but that
doesn't solve this problem.

As you said in the beginning, you need to either change one of the names
or use the qualified name B::i whenever you refer to i.

--

-- Pete
Roundhouse Consulting, Ltd. -- www.versatilecoding.com
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
Nov 11 '06 #10

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

Similar topics

18
1961
by: George Sakkis | last post by:
I'm looking for a design to a problem I came across, which goes like this (no, it's not homework): 1. There is a (single inheritance) hierarchy of domain classes, say A<-B<-..<-Z (arrows point to the parent in the inheritance tree). 2. This hierarchy evolved over time to different versions for each class. So for example, version's 1 hierarchy would be A_v1 <-B_v1 <-..<-Z_v1. 3. At runtime, only one version is selected by a factory...
2
4324
by: Graham Banks | last post by:
Does using multiple inheritance introduce any more performance overhead than single inheritance?
5
2171
by: Morgan Cheng | last post by:
It seems no pattern defined by GoF takes advantage of multiple inheritance. I am wondering if there is a situation where multiple inheritance is a necessary solution. When coding in C++, should multiple inheritance still be avoided? If yes, why multiple inheritance is introducted into C++?
3
2464
by: Morten Aune Lyrstad | last post by:
Hi again! I'm having problems with inheritance. I have a base interface class called IObject. Next I have two other interfaces classes, IControl and ICommandMaster, which derives from IObject. My problem is that I have a /third/ class, CCommand, which derives from both IControl and ICommandmaster... The error message says (Weird.....)
20
10057
by: km | last post by:
Hi all, In the following code why am i not able to access class A's object attribute - 'a' ? I wishto extent class D with all the attributes of its base classes. how do i do that ? thanks in advance for enlightment ... here's the snippet #!/usr/bin/python
22
23337
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete examples?
47
3622
by: Mark | last post by:
why doesn't .NET support multiple inheritance? I think it's so silly! Cheers, Mark
60
4898
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the 'target' programming community herein) to get some community input and verify (or not) the following two statements. Few programmers (3 to7%) UNDERSTAND 'Strategic Functional Migration
2
2556
by: Paul McGuire | last post by:
On May 25, 8:37 am, Michael Hines <michael.hi...@yale.eduwrote: Here's a more general version of your testing code, to detect *any* diamond multiple inheritance (using your sample classes). -- Paul for cls in (A,B,C,D): seen = set()
0
7946
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8251
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
6654
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5739
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5408
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3859
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2385
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1478
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1210
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.