Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > // how can I access A::a?

Reply
Thread Tools

// how can I access A::a?

 
 
dick
Guest
Posts: n/a
 
      04-11-2011
/* multi_level_inheritance.cpp */
struct A
{
int a;
};

struct B1 : public A
{
int a;
};

struct B2 : public A
{
int a;
};

struct C : public B1, public B2
{
int a;
};

int main()
{
C ccc;

ccc.a=123;

ccc.B1::a=456;

/* 0030 */ ccc.B1::A::a=789;

}

// c++ multi_level_inheritance.cpp
// multi_level_inheritance.cpp: In function int main():
// multi_level_inheritance.cpp:30: error: A is an ambiguous base of C


// how can I access A::a?
 
Reply With Quote
 
 
 
 
Carlo Milanesi
Guest
Posts: n/a
 
      04-11-2011
On 12/04/2011 0.52, dick wrote:
> struct A
> {
> int a;
> };
>
> struct B1 : public A
> {
> int a;
> };
>
> struct B2 : public A
> {
> int a;
> };
>
> struct C : public B1, public B2
> {
> int a;
> };


If I go on with:

#include <iostream>
int main() {
C ccc;
ccc.a=123;
ccc.B1::a=234;
ccc.B2::a=345;
ccc.B1::A::a=456;
ccc.B2::A::a=567;
std::cout << ccc.a << " " << ccc.B1::a << " "
<< ccc.B2::a << " " << ccc.B1::A::a << " "
<< ccc.B2::A::a;
}

.... and then I compile and run this program, I get as output:
123 234 345 456 567

What's the problem?

--

Carlo Milanesi
http://carlomilanesi.wordpress.com/
 
Reply With Quote
 
 
 
 
dick
Guest
Posts: n/a
 
      04-11-2011
>
> What's the problem?
>


My g++ compiler did not compile the code.


What compiler did you use?

 
Reply With Quote
 
Carlo Milanesi
Guest
Posts: n/a
 
      04-12-2011
On 12/04/2011 1.56, dick wrote:
>>
>> What's the problem?
>>

>
> My g++ compiler did not compile the code.
>
>
> What compiler did you use?


I used Microsoft (R) 32-bit C/C++ Optimizing Compiler version
15.00.30729.01 for 80x86, included with Visual Studio 2008.
Now I just tested it also with g++ 4.3.2 for Linux and actually I got
for 4 times the compilation error: ‘A’ is an ambiguous base of ‘C’.

I think GCC is wrong.

--

Carlo Milanesi
http://carlomilanesi.wordpress.com/
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      04-12-2011
On 4/12/2011 7:38 AM, Pete Becker wrote:
> On 2011-04-11 18:52:40 -0400, dick said:
>
>> /* multi_level_inheritance.cpp */
>> struct A
>> {
>> int a;
>> };
>>
>> struct B1 : public A
>> {
>> int a;
>> };
>>
>> struct B2 : public A
>> {
>> int a;
>> };
>>
>> struct C : public B1, public B2
>> {
>> int a;
>> };
>>
>> int main()
>> {
>> C ccc;
>>
>> ccc.a=123;
>>
>> ccc.B1::a=456;
>>
>> /* 0030 */ ccc.B1::A::a=789;
>>
>> }
>>
>> // c++ multi_level_inheritance.cpp
>> // multi_level_inheritance.cpp: In function int main():
>> // multi_level_inheritance.cpp:30: error: A is an ambiguous base of C
>>
>>
>> // how can I access A::a?

>
> There are two A::a's, which is why the error message said that the
> reference to A is ambiguous. You need to decide which one you want to
> access, then name it. Hint: there's one in B1 and one in B2.


And does the fact that the OP specifically used 'B1::' before 'A::a'
matter not?

V
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      04-12-2011
On 4/12/2011 2:20 PM, Pete Becker wrote:
> On 2011-04-12 13:34:19 -0400, Victor Bazarov said:
>
>> On 4/12/2011 7:38 AM, Pete Becker wrote:
>>> On 2011-04-11 18:52:40 -0400, dick said:
>>>
>>>> /* multi_level_inheritance.cpp */
>>>> struct A
>>>> {
>>>> int a;
>>>> };
>>>>
>>>> struct B1 : public A
>>>> {
>>>> int a;
>>>> };
>>>>
>>>> struct B2 : public A
>>>> {
>>>> int a;
>>>> };
>>>>
>>>> struct C : public B1, public B2
>>>> {
>>>> int a;
>>>> };
>>>>
>>>> int main()
>>>> {
>>>> C ccc;
>>>>
>>>> ccc.a=123;
>>>>
>>>> ccc.B1::a=456;
>>>>
>>>> /* 0030 */ ccc.B1::A::a=789;
>>>>
>>>> }
>>>>
>>>> // c++ multi_level_inheritance.cpp
>>>> // multi_level_inheritance.cpp: In function int main():
>>>> // multi_level_inheritance.cpp:30: error: A is an ambiguous base of C
>>>>
>>>>
>>>> // how can I access A::a?
>>>
>>> There are two A::a's, which is why the error message said that the
>>> reference to A is ambiguous. You need to decide which one you want to
>>> access, then name it. Hint: there's one in B1 and one in B2.

>>
>> And does the fact that the OP specifically used 'B1::' before 'A::a'
>> matter not?
>>

>
> Sure, but you're giving away the ending. There's clearly a problem in
> the line ccc.a = 123; so it's not at all likely that the only error
> message came from the line marked /* 0030 */.


Actually, since 'C' has a member 'a', which hides *both* 'B1::a' and
'B2::a' (which in turn hid both 'A::a' from base class 'A'), I would not
expect to see an error in 'ccc.a = 123;'. Just saying...

>
> On the other hand, it does help to carefully read the code in question.
>


V
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
Miles Bader
Guest
Posts: n/a
 
      04-13-2011
Carlo Milanesi <> writes:
> I think GCC is wrong.


Note that both clang++ (2. and Comeau C++'s online compiler[*] (which
seems to be highly regarded for accurately reflecting the standard) give
the same error as gcc...
[*] http://www.comeaucomputing.com/tryitout

-Miles

--
My books focus on timeless truths. -- Donald Knuth
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Can't access laptop from desktop, but can access desktop from lapt =?Utf-8?B?Y2FydG1hbg==?= Wireless Networking 2 08-06-2005 01:08 AM
wireless can't access wired. But Wired can access wireless =?Utf-8?B?ZGZhdG92aWM=?= Wireless Networking 5 02-05-2005 08:07 AM
one computer can access shared files the other can't even access =?Utf-8?B?SiBIYW1lcg==?= Wireless Networking 4 12-07-2004 03:13 AM
Desktop can't access Laptop but Laptop can access desktop =?Utf-8?B?Qmx1Y2FkZHk3MQ==?= Wireless Networking 2 11-23-2004 01:52 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57