Topic: Accessing members of indirect base class


Author: bill@amber.ssd.hcsc.com (Bill Leonard)
Date: 1996/07/01
Raw View
I am not sure if the following code is legal -- my C++ compiler says it
is not, but I don't see why it should be illegal.  I freely admit I
cannot find anything in the draft standard that unequivocally makes it
legal, either.  Here's the code:

   float   tempf = -1.0;

   class DupBase {
   public:
      int      x;
      float    y;

   };

   class Middle1 : public DupBase {
   public:
      float    x;
      int      y;
   };

   class Middle2 : public DupBase {
   public:
      char       x;
      short      y;
   };

   class Derived : public Middle1, public Middle2 {
   public:
      void
      memfunc(void);

      int     v;
   };

   void
   Derived::memfunc(void) {
      tempf = Middle1::DupBase::y;      // Is this legal?
   }

If this is *not* legal, then how does memfunc access the "y" member of the
DupBase sub-object of the Middle1 sub-object of Derived?

Note: In this particular case, it works to first cast "this" to "Middle1 *",
like so:

   void
   Derived::memfunc(void) {
      Middle1   * mp = (Middle1 *) this;
      tempf = mp->DupBase::y;
   }

However, this technique would not work if "y" were a protected member (I
think), because protected members of an object can only be accessed in a
member function if the object is known to be of the same (or derived from
the) class containing the member function.  (Hope I said that right.)

The standard does not explicitly say whether "DupBase" is considered to
be a name that can be referenced in the scope of Middle1, but it does say
that the name after "Middle1::" will be looked up in the scope of Middle1.

On the other hand, a member function of Middle1 can reference "DupBase::y",
indicating that, in this context at least, DupBase is a visible name in the
scope of Middle1.  So if my example is illegal, the standard seems to be
inconsistent in the scoping of base-class names.

--
Bill Leonard
Concurrent Computer Corporation
2101 W. Cypress Creek Road
Fort Lauderdale, FL  33309
Bill.Leonard@mail.hcsc.com

These opinions and statements are my own and do not necessarily reflect the
opinions or positions of Concurrent Computer Corporation.
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: Alexander Krotoff <krotoff@boy.nmd.msu.ru>
Date: 1996/07/02
Raw View
In article <4r9f10$896@ns.hcsc.com> you wrote:
> I am not sure if the following code is legal -- my C++ compiler says it
> is not, but I don't see why it should be illegal.  I freely admit I
> cannot find anything in the draft standard that unequivocally makes it
> legal, either.  Here's the code:

>    float   tempf = -1.0;

>    class DupBase {
>    public:
>       int      x;
>       float    y;

>    };

>    class Middle1 : public DupBase {
>    public:
>       float    x;
>       int      y;
>    };

>    class Middle2 : public DupBase {
>    public:
>       char       x;
>       short      y;
>    };

>    class Derived : public Middle1, public Middle2 {
>    public:
>       void
>       memfunc(void);

>       int     v;
>    };

>    void
>    Derived::memfunc(void) {
>       tempf = Middle1::DupBase::y;      // Is this legal?

Not, it is not legal. Middle1::DupBase::y will only be legal, if
DupBase will be a nested class of the class Middle1.
You may try `tempf = Middle1::y;'.
--
Alexander N. Krotoff  krotoff@such.srcc.msu.su
Research Computer Center tel: +7(095)939-2638
Moscow State University  fax: +7(095)939-4430
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]