Topic: Name lookup for base class (aka strange thing)


Author: "Eugene Radchenko" <eugene@qsar.chem.msu.su>
Date: 1996/02/12
Raw View
Hi people!
I am not sure if this is on-topic but it has something to do with standard
conformance after all.
I just stumbled on a strange problem and would like to know if it is
compiler (BC++ 4.0) bug or mine...
The code snippet:
  class A {
   protected:
    class a { };
  };
  class B : public A {
   protected:
    class b : public a { };  //Error here: A::a not accessible
  };
I would expect A::a to be protected in class B and thus accessible.
(Making class 'b' a friend of 'B' does not help either...)

            bye                  Genie
--
-----------------------------------------------------------------------
Eugene V. Radchenko              Research associate, Computer Chemistry
E-mail: eugene@qsar.chem.msu.su                   Fax: +7-(095)939-0290
Ordinary mail:     Chair of Organic Chemistry, Department of Chemistry,
                         Moscow State University, 119899 Moscow, Russia
## The careful application of terror is also a form of communication ##
---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy is
  in http://reality.sgi.com/employees/austern_mti/std-c++/policy.html. ]





Author: Bowden Wise <wiseb@cs.rpi.edu>
Date: 1996/02/14
Raw View
Eugene Radchenko wrote:
>
> I just stumbled on a strange problem and would like to know if it is
> compiler (BC++ 4.0) bug or mine...
> The code snippet:
>   class A {
>    protected:
>     class a { };
>   };
>   class B : public A {
>    protected:
>     class b : public a { };  //Error here: A::a not accessible
>   };
> I would expect A::a to be protected in class B and thus accessible.
> (Making class 'b' a friend of 'B' does not help either...)

Note that you are not providing any constructors for classes 'a' and
'b', so the compiler will provide them for you. These will be
public so you should be able to derive from a:

   class b : public a {};

your example compiles with no errors using BC++ 4.5 so it must be
a bug with BC++ 4.0.  You might try adding constructors:

class A
{
protected:
 class a { public: a() {} };
};

class B : public A
{
protected:
   class b : public a { public: b():a() {} };  // Error here
};

Interestingly, with BC++ 4.5 I now get the error that
  A::a is not accessible

I looked through the DWP and ARM and cannot quite figure out
why this is the case.

I suppose in the first case, when there are no explicit constructors
declared for class b or class a, the base class is NOT called by
b()?  So the code compiles?  But when I add them it complains.

Why isn't class a in the scope of class B?

--------------------------------------------------------------------
G. Bowden Wise
Computer Science Dept, Rensselaer Polytechnic Inst, Troy, NY 12180
Email: wiseb@cs.rpi.edu         WWW: http://www.cs.rpi.edu/~wiseb/
---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  Moderation policy:
  http://reality.sgi.com/employees/austern_mti/std-c++/policy.html. ]





Author: jpotter@falcon.lhup.edu (John E. Potter)
Date: 1996/02/20
Raw View
"Problem" duplicated with IBM xlC.

class B {
 protected :
  struct B1 {
    int i;
   };
 };
class D : public B {
 friend struct D1;
 public :
  D::B1 b1; // ok B::B1 is accessable to D.
 protected :
  struct D1 : public D::B1 {
    // error: protected member "B::B1" cannot be accessed.
   };
 };
D::B1 b1; // error: protected member "B::B1" cannot be accessed.
D d;
int x(d.b1.i);  // ok d.b1 is public and so is d.b1.i.


ARM pg187: "Member functions of a nested class have no special access to
members of an enclosing class; they obey the usual access rules".

The nested class declaration has no more right to inherit from the
protected types of the enclosing class than do its member functions
to access the protected members.  Although not explicitly stated, this
seems consistent.  Expert opinion appreciated.

For a concrete example, consider Stroustrup, "The C++ Programming Language",
Section 8.3.  Let B == slist_base, B1 == slink, D == Slist, D1 = Tlink.
In that case, it is D : private B and the error messages change to
private member "B::B1".

It seems like a good idea to move the links into the protected areas of
the lists; however, it is not possible.

Enjoy,
John
---
[ To submit articles: Try just posting with your newsreader.  If that fails,
                      use mailto:std-c++@ncar.ucar.edu
  FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  Comments? mailto:std.c++-request@ncar.ucar.edu
]