Topic: Inheriting structures


Author: Jason Merrill <jason@cygnus.com>
Date: 1997/05/25
Raw View
>>>>> Robert J Macomber <robertm@helium.gas.uug.arizona.edu> writes:

> Visual C++ 5.0 and 4.0 reject this program at the commented line, saying I
> can't access foo because it's protected (actually, it says that if I write
> struct bar:public Base::foo -- as it stands it just says "base class foo
> undefined).  g++ 2.7.2 allows this with no problem, and runs exactly as
> expected.  Which is correct?

VC++.  g++ does not currently do access control on nested types.

Jason
---
[ 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         ]
[ 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                             ]





Author: jpotter@falcon.lhup.edu (John Potter)
Date: 1997/05/28
Raw View
On 24 May 97 06:37:06 GMT, Robert J Macomber wrote:

: What does the C++ standard say about inheriting protected structures?

Jason answered your question; so, lets explain why and fix your code.

: #include <iostream.h>

: class Base {
: protected:
:   struct foo {
:     virtual void i_am() { cout << "I am a base" << endl; }
:   }* mFoo;
:   Base(foo* y):mFoo(y) {}
: public:
:   Base() { mFoo=new foo; }
:   virtual ~Base() { delete mFoo; }
:   void whatAmI() { mFoo->i_am(); }
: };

: class Derived:public Base {
: protected:

Derived has access to the protected parts of Base; however, bar does
not have access to Derived.  Let's give it access.

 struct bar;  // There is a nested struct
 friend bar;  // And it is a friend

Now bar has access to Derived and can derive from Base::foo.

:   struct bar:public foo {  // Here's the problem line
:     void i_am() { cout << "I am derived" << endl; }
:   };
: public:
:   Derived():Base(new bar) {}
: };
---
[ 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         ]
[ 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                             ]





Author: robertm@helium.gas.uug.arizona.edu (Robert J Macomber)
Date: 1997/05/24
Raw View
What does the C++ standard say about inheriting protected structures?
Say I've got a program like this (a somewhat contrived example to
illustrate the problem; I'm actually trying to provide a consistent
interface to the three or so mutex structures provided by Windows NT):

#include <iostream.h>

class Base {
protected:
  struct foo {
    virtual void i_am() { cout << "I am a base" << endl; }
  }* mFoo;
  Base(foo* y):mFoo(y) {}
public:
  Base() { mFoo=new foo; }
  virtual ~Base() { delete mFoo; }
  void whatAmI() { mFoo->i_am(); }
};

class Derived:public Base {
protected:
  struct bar:public foo {  // Here's the problem line
    void i_am() { cout << "I am derived" << endl; }
  };
public:
  Derived():Base(new bar) {}
};

int main() {
  Base b;
  Derived d;

  b.whatAmI();
  d.whatAmI();

  return 0;
}

Visual C++ 5.0 and 4.0 reject this program at the commented line, saying I
can't access foo because it's protected (actually, it says that if I write
struct bar:public Base::foo -- as it stands it just says "base class foo
undefined).  g++ 2.7.2 allows this with no problem, and runs exactly as
expected.  Which is correct?
--
   Rob Macomber
    (robertm@gas.uug.arizona.edu)
---
[ 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         ]
[ 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                             ]