Topic: Multiple inheritance and base class access with repeated bases
Author: "Dave J.G." <see@signa.ture>
Date: 1998/05/19 Raw View
While testing an object streaming class I've been working on, I came up with this
monster:
Class A { public: int i ; } ;
Class B : public A { public: int i ; } ;
Class C : public A, public B { public: int i ; } ;
Class D : public A, public B, public C { public: int i ; } ;
This compiles okay ( with a 'repeated base' warning and a couple of derogatory
comments from the compiler ) but when I try to access D::A::i in a method ( not
shown ) of Class D, I get an ambiguous base error. I am also having problems with
my compiler ( Borland C++Builder 3 ) not accepting the use of multiple scope
resolution operators such as B::A. It doesn't recognize 'A' as a 'namespace or
class'.
It seems to me that I should be able to access every 'i' in this hierarchy from 'D'
by using the appropriate scope resolution operators. However, I can't seem to do
this with C++Builder which is supposed to be ANSI/ISO compliant. Am I wrong in
thinking that I can access these base classes in this way? As a C++ devotee, I
refuse to believe that A's 'i' in the immediate base of D is lost forever due to
ambiguity. Also, I know I've used multiple scope resolution operators in the past (
with Borland C++5.02 ) to access base class members and this method is supported in
Bjarne Stroustrup's C++PL3 ( p. 394, sect 15.2.3 ). It doesn't seem to be working
with C++Builder though.
Thanks,
Dave
P.S.
no need to CC to my e-mail address but if you do, make sure you use the one
encoded below.
--
( reply-to address changed to avoid the spammers,
use the following e-mail address )
daveg T unpronounceable D T com
http://www.unpronounceable.com/daves
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1998/05/20 Raw View
Dave J.G. wrote:
> While testing an object streaming class I've been working on, I came up with this
> monster:
>
> Class A { public: int i ; } ;
> Class B : public A { public: int i ; } ;
> Class C : public A, public B { public: int i ; } ;
> Class D : public A, public B, public C { public: int i ; } ;
You don't need to go that far to have problems. If you stop with C, you
already have an ambiguity.
> This compiles okay ( with a 'repeated base' warning and a couple of derogatory
> comments from the compiler ) but when I try to access D::A::i in a method ( not
> shown ) of Class D, I get an ambiguous base error. I am also having problems with
> my compiler ( Borland C++Builder 3 ) not accepting the use of multiple scope
> resolution operators such as B::A. It doesn't recognize 'A' as a 'namespace or
> class'.
>
> It seems to me that I should be able to access every 'i' in this hierarchy from 'D'
> by using the appropriate scope resolution operators.
No, you can't access some of them. That's because the base class
C::A is ambiguous. There is no way to refer to it that wouldn't
also refer to C::B::A.
--
Valentin Bonnard mailto:bonnardv@pratique.fr
info about C++/a propos du C++: http://pages.pratique.fr/~bonnardv/
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: JHB NIJHOF <nijhojhb@aston.ac.uk>
Date: 1998/05/20 Raw View
Dave J.G. <see@signa.ture> wrote:
: While testing an object streaming class I've been working on, I came up with this
: monster:
: Class A { public: int i ; } ;
: Class B : public A { public: int i ; } ;
: Class C : public A, public B { public: int i ; } ;
: Class D : public A, public B, public C { public: int i ; } ;
: This compiles okay ( with a 'repeated base' warning and a couple of
derogatory : comments from the compiler ) but when I try to access
D::A::i in a method ( not : shown ) of Class D, I get an ambiguous
base error.
No wonder. Class has 8 members i, four of which which are
from class A:
A = (Ai)
B = ((Ai) Bi)
C = ((Ai) ((Ai) Bi) Ci)
D = ((Ai) ((Ai) Bi) ((Ai) ((Ai) Bi) Ci))
So the compiler cannot figure out which of the i-s from class A you want.
Try virtual base clases.
--
Jeroen Nijhof J.H.B.Nijhof@aston.ac.uk
Accordion Links http://www-th.phys.rug.nl/~nijhojhb/accordions.html
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1998/05/20 Raw View
Dave J G <see@signa.ture> writes:
> Class A { public: int i ; } ;
> Class B : public A { public: int i ; } ;
> Class C : public A, public B { public: int i ; } ;
> Class D : public A, public B, public C { public: int i ; } ;
> This compiles okay [...] but when I try to access D::A::i [...] I
> get an ambiguous base error.
The Nov'97 DWP states, in [class.mi]/3:
[...] a class can be an indirect base class
more than once and can be a direct and an indirect base class. There
are limited things that can be done with such a class. The non-static
data members and member functions of the direct base class cannot be
referred to in the scope of the derived class.
So, you can't disambiguate D::A, because this could be interpreted as
D::A, D::B::A, D::C::A, D::C::B::A.
--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]