[LC++]Pure base class as a friend

Carlo Wood carlo at alinoe.com
Fri Sep 7 14:09:07 UTC 2001

I don't like the design really, but when I had this problem I'd
probably do this:

class Point {
friend class Manipulator;
  int x, y;

class Manipulator {
  void set_x(Point &p, int x) { p.x = x; }
  void set_y(Point &p, int y) { p.y = y; }
  int get_x(Point &p) { return p.x; }
  int get_y(Point &p) { return p.y; }
  // ...

class SomeManipulator : public Manipulator {
  void swap_points(Point &p1, Point &p2)
    int x = get_x(p1);
    int y = get_y(p1);
    set_x(p1, get_x(p2));
    set_y(p1, get_y(p2));
    set_x(p2, x);
    set_y(p2, y);

That any class derived from Manipulator has access
to all and any Point, but no other classes do.

On Fri, Sep 07, 2001 at 12:50:54PM +0930, Mark Phillips wrote:
> Hi,
> Suppose I have a class "pointTy" which stores a point on a sphere.  And
> suppose I have a pure base class "manipulatorTy" which allows the
> manipulation of these points.  The idea is that there might be various
> different types of manipulation, expressed via derived classes from this
> base class.  What I would like to be able to do, is say manipulatorTy
> is a friend of pointTy.  So that any manipulator would be able to
> change private members of pointTy.  But my understanding is that
> only the base class would be a friend of pointTy.  Any classes derived
> from manipulatorTy would not be friends unless they were each explicitly
> declared friend (which is not a good idea).  Am I right about this?
> At the moment, the only way around my dilemma is to change some of the
> pointTy private members into public members.  But I'd prefer not to do
> this if I can.
> I could make manipulatorTy a derivation of pointTy, and make the
> internals of pointTy "protected", but this won't work because I want
> manipulators to be able to work on more than one point, and to change
> the points they are working on.
> Any ideas?
> Cheers,
> Mark.
> _______________________________________________
> This is the Linux C++ Programming List
> : http://lists.linux.org.au/listinfo/tuxcpprogramming List

Carlo Wood <carlo at alinoe.com>

More information about the tuxCPProgramming mailing list