Article View: comp.lang.c++.moderated
Article #65Problem with the relationship between friend and derived classes
From: Christopher Kline
Date: Sun, 31 Mar 1996 17:21
Date: Sun, 31 Mar 1996 17:21
126 lines
3496 bytes
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 ]
Technical Details
Message-ID:
<4jmev1$c0r@netlab.cs.rpi.edu>Path:
arc1-ca.pugleaf.net!news-1.csn.net!ub!newserve!rebecca!rpi!not-for-mailShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!news-1.csn.net!ub!newserve!rebecca!rpi!not-for-mail From: Christopher Kline <ckline@tc.cornell.edu> Newsgroups: comp.lang.c++.moderated,comp.lang.c++ Subject: Problem with the relationship between friend and derived classes Date: 31 Mar 1996 17:21:37 -0000 Organization: Cornell University Sender: cppmods@netlab.cs.rpi.edu Approved: Dietmar.Kuehl@uni-konstanz.de Message-ID: <4jmev1$c0r@netlab.cs.rpi.edu> NNTP-Posting-Host: netlab.cs.rpi.edu X-Original-Date: Sat, 30 Mar 1996 20:24:50 -0500