Topic: making selected members public with using?


Author: Steve Clamage <stephen.clamage_nospam@Eng.Sun.COM>
Date: 1997/09/11
Raw View

Christopher Eltschka wrote:
>
> Is it possible to make just a few member function from a private
> base class public with the following code?
>
> class Base
> {
> public:
>   void f();
>   void g();
> };
>
> class Derived: private Base
> {
> public:
>   using Base::f; // make B::f public?
> };

Yes. The old way to perform this operation was an "access declaration":
 class Derived : private Base {
 public:
  Base::f;
 };
but using-declarations are now preferred. They have nicer overall
semantics and are more flexible.

--
Steve Clamage, stephen.clamage_nospam@eng.sun.com
( Note: remove "_nospam" when replying )
---
[ 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: bs@research.att.com (Bjarne Stroustrup)
Date: 1997/09/11
Raw View
Christopher Eltschka <celtschk@physik.tu-muenchen.de> writes:

 > Is it possible to make just a few member function from a private
 > base class public with the following code?
 >
 > class Base
 > {
 > public:
 >   void f();
 >   void g();
 > };
 >
 > class Derived: private Base
 > {
 > public:
 >   using Base::f; // make B::f public?
 > };
 >
 > int main()
 > {
 >   Derived d;
 >   d.f(); // Ok?
 >   d.g(); // Error: g() is private
 >   return 0;
 > }

Yes. That was always possible in C++. Early versions simply used the simple
(and in my opinion less obvious) syntax:

 class Derived: private Base
 {
 public:
  Base::f; // make B::f public
 };

Using declarations can also be used to resolve ambiguities from base classes.
For example

 class B1 {
 public:
  int f(int);
  // ...
 };

 class B2 {
 public:
  double f(double);
  // ...
 };

 class D : public B1, public B2 {
 public:
  using B1::f;
  using B2::f;
  // ...
 };

 void f(D* p)
 {
  int i = p->f(1); // call B1's f
  double d = p->f(2.0); // call B2's f
  // ...
 }

Look for 'using' in the index of "The C++ Programming Language (3rd Edition).

 - Bjarne

Bjarne Stroustrup, AT&T Labs, http://www.research.att.com/~bs
---
[ 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: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1997/09/10
Raw View
Is it possible to make just a few member function from a private
base class public with the following code?

class Base
{
public:
  void f();
  void g();
};

class Derived: private Base
{
public:
  using Base::f; // make B::f public?
};

int main()
{
  Derived d;
  d.f(); // Ok?
  d.g(); // Error: g() is private
  return 0;
}
---
[ 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                             ]