🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.lang.c++.moderated
1 messages
1 total messages Started by Christopher Kline Sun, 31 Mar 1996 17:21
Problem with the relationship between friend and derived classes
#65
Author: Christopher Kline
Date: Sun, 31 Mar 1996 17:21
126 lines
3496 bytes
I have a problem involving the relationship between friend classes and
derived classes. 

My group is currently writing a simulation framework involving moving
vehicles, each of which have an associated set of dynamics which
control their movement and their interaction with other vehicles in
the simulation. 

The problem I am having can be seen in this minimal example (the
errors are included as comments at point where they occur):


class engine { 

public:
  engine(car *c) {
    attachedCar = c;
  };
  
  virtual void update(void) = 0;  

protected:
  car *attachedCar;  
};

//--------------------

class car {

  friend engine;
  
public:
  car(void) {
    velocity = 0;
  }

  virtual void move(void) = 0;

protected:
  engine *e;
  double velocity;
  
};

// -------------------

class honda_engine : public engine {

public:
  honda_engine(honda *h)
    : engine(h) { }
  
  void update(void) {

    // Error #1: "engine::attachedCar" is inaccessible   
    // Error #2: "car::velocity" is inaccessible
    attachedCar->velocity++; 
			     
    // Error #3: "honda_miles" is undefined
    honda_miles++;	     
  }

};

//--------------------

class honda : public car {

  friend honda_engine;

public:
  honda(void) {
    honda_miles = 0;
    e = new honda_engine(this);
  }

  void move(void) {
    e->update();
  }

private:
  int honda_miles;
  
};

//--------------------


Engines are friends of their respective cars and the base classes for
engines and cars are both abstract classes (hence only instances of
the derived classes will be created). 

The problem is that I would like:

1) a derived engine to have access to the protected members of the
derived car class of which it is a friend, and of the parent class(es)
of that car.

2) a derived engine to access the correct class of car when it
dereferences the attachedCar member (attachedCar will always be
something derived from car, even if the compiler can't understand
that).


I understand WHY these three errors are occurring, but I can't figure
out HOW to get my code to behave the way I want it to (i.e., using the
relationship between cars/engines that I've set up).

Does anyone have any clue on how to get this to work? It seems like it
would be a fairly common design problem, but I haven't found reference
to it in any of my books or the FAQ.

Any help would be greatly appreciated. Also, if you would kindly CC:
me on the reply, that would be great, in case I miss the post in the
newsgroup.

-- 
Christopher Kline 
http://www.tc.cornell.edu/~ckline/

to compose, Decompose, I wander and slip

      [ Articles to moderate: mailto:c++-submit@netlab.cs.rpi.edu ]
      [  Read the C++ FAQ: http://www.connobj.com/cpp/cppfaq.htm  ]
      [  Moderation policy: http://www.connobj.com/cpp/guide.htm  ]
      [      Comments? mailto:c++-request@netlab.cs.rpi.edu       ]
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads