Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #3429 > unrolled thread

// how can I access A::a?

Started bydick <dick_12345678@hotmail.com>
First post2011-04-11 15:52 -0700
Last post2011-04-12 15:13 -0400
Articles 10 — 5 participants

Back to article view | Back to comp.lang.c++


Contents

  // how can I access A::a? dick <dick_12345678@hotmail.com> - 2011-04-11 15:52 -0700
    Re: // how can I access A::a? Carlo Milanesi <carlo.nospam.milanesi@libero.it> - 2011-04-12 01:08 +0200
      Re: // how can I access A::a? dick <dick_12345678@hotmail.com> - 2011-04-11 16:56 -0700
        Re: // how can I access A::a? Carlo Milanesi <carlo.nospam.milanesi@libero.it> - 2011-04-12 18:46 +0200
          Re: // how can I access A::a? Miles Bader <miles@gnu.org> - 2011-04-13 15:57 +0900
    Re: // how can I access A::a? Pete Becker <pete@versatilecoding.com> - 2011-04-12 07:38 -0400
      Re: // how can I access A::a? Victor Bazarov <v.bazarov@comcast.invalid> - 2011-04-12 13:34 -0400
        Re: // how can I access A::a? Pete Becker <pete@versatilecoding.com> - 2011-04-12 14:20 -0400
          Re: // how can I access A::a? Victor Bazarov <v.bazarov@comcast.invalid> - 2011-04-12 15:01 -0400
            Re: // how can I access A::a? Pete Becker <pete@versatilecoding.com> - 2011-04-12 15:13 -0400

#3429 — // how can I access A::a?

Fromdick <dick_12345678@hotmail.com>
Date2011-04-11 15:52 -0700
Subject// how can I access A::a?
Message-ID<bee3f0ea-42ae-4ae4-a254-d5bc9e004f93@k5g2000yqj.googlegroups.com>
        /* 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?

[toc] | [next] | [standalone]


#3430

FromCarlo Milanesi <carlo.nospam.milanesi@libero.it>
Date2011-04-12 01:08 +0200
Message-ID<4da389ea$0$30912$5fc30a8@news.tiscali.it>
In reply to#3429
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/

[toc] | [prev] | [next] | [standalone]


#3435

Fromdick <dick_12345678@hotmail.com>
Date2011-04-11 16:56 -0700
Message-ID<5de50c3f-7cb2-4133-b17f-38eb59e4e6e7@d28g2000yqc.googlegroups.com>
In reply to#3430
>
> What's the problem?
>

My g++ compiler did not compile the code.


What compiler did you use?

[toc] | [prev] | [next] | [standalone]


#3493

FromCarlo Milanesi <carlo.nospam.milanesi@libero.it>
Date2011-04-12 18:46 +0200
Message-ID<4da481c5$0$6833$5fc30a8@news.tiscali.it>
In reply to#3435
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/

[toc] | [prev] | [next] | [standalone]


#3537

FromMiles Bader <miles@gnu.org>
Date2011-04-13 15:57 +0900
Message-ID<buolized4g1.fsf@dhlpc061.dev.necel.com>
In reply to#3493
Carlo Milanesi <carlo.nospam.milanesi@libero.it> writes:
> I think GCC is wrong.

Note that both clang++ (2.8) 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

[toc] | [prev] | [next] | [standalone]


#3462

FromPete Becker <pete@versatilecoding.com>
Date2011-04-12 07:38 -0400
Message-ID<2011041207385159183-pete@versatilecodingcom>
In reply to#3429
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.

-- 
  Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The 
Standard C++ Library Extensions: a Tutorial and Reference 
(www.petebecker.com/tr1book)

[toc] | [prev] | [next] | [standalone]


#3497

FromVictor Bazarov <v.bazarov@comcast.invalid>
Date2011-04-12 13:34 -0400
Message-ID<io22er$be8$1@dont-email.me>
In reply to#3462
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

[toc] | [prev] | [next] | [standalone]


#3506

FromPete Becker <pete@versatilecoding.com>
Date2011-04-12 14:20 -0400
Message-ID<2011041214200693759-pete@versatilecodingcom>
In reply to#3497
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 */.

On the other hand, it does help to carefully read the code in question. :-(

-- 
  Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The 
Standard C++ Library Extensions: a Tutorial and Reference 
(www.petebecker.com/tr1book)

[toc] | [prev] | [next] | [standalone]


#3514

FromVictor Bazarov <v.bazarov@comcast.invalid>
Date2011-04-12 15:01 -0400
Message-ID<io27j5$3v1$1@dont-email.me>
In reply to#3506
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

[toc] | [prev] | [next] | [standalone]


#3515

FromPete Becker <pete@versatilecoding.com>
Date2011-04-12 15:13 -0400
Message-ID<2011041215133973606-pete@versatilecodingcom>
In reply to#3514
On 2011-04-12 15:01:57 -0400, Victor Bazarov said:

> 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. :-(
>> 
> 

It does help to carefully read the code in question. :-(

-- 
  Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The 
Standard C++ Library Extensions: a Tutorial and Reference 
(www.petebecker.com/tr1book)

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web