0

*I'm using "Module" to mean some extension of a class, either through inheritance or composition.

Say I have one of the following declarations of an Entity:

using EUnit = int;
class Entity : /*With inheritance*/
 Positional<EUnit> {
public:
 Entity();
};

or

class Entity { /*With composition*/
 Position<EUnit> pos; //Same as "Positional", but renamed
public:
 Entity();
};

And the declaration of Position/Positional:

template <class T>
class Positional {
 T xPos;
 T yPos;
public:
 Positional();
 Positional(T x, T y);
 void moveTo(T x, T y);
 void moveBy(T xOff, T yOff);
 T getX() const;
 T getY() const;
};

What if I want to create a class called RandomWalk that can be used to cause the Entity (or any other Positional class) to randomly move to a location?

Going the inheritance route, I thought of having RandomWalk inherit from Positional so it has access to a position to act on. If I then set up Entity like:

class Entity :
 Positional<EUnit>, virtual RandomWalk<EUnit> { ...

RandomWalk now how access to Entity's position, but it involved "diamond inheritance". I'm virtually inheriting RandomWalk, so only one version of a Positional should exist, but multiple inheritance like this still seems to be frowned upon from what I've read.

That leaves me with composition. The only way I could think of giving RandomWalk access to the Entity's position this way was to do something like pass a reference/pointer to the Entity's Positional to the RandomWalk constructor. Using pointers for something like this seems like overkill though.

And now I'm out of ideas, and I'd like suggestions/advice (or just general comments on the problem).

I know I could just directly add a randomWalk function to Entity, but I'm looking at this through the perspective of re-use.

And yes, RandomWalk is kind of a dumb use-case, but I'm sure this problem (or a similar one) occurs in the real world.

asked May 13, 2015 at 14:19

1 Answer 1

1

Some alternatives:

  1. Have RandomWalk inherit from Positional. With this definition, a RandomWalk is a Positional entity which can walk randomly or to specific locations. I don't really like this approach; it leaves no way to handle arbitrary permutations walkers (e.g., RandomWalk, SquareDanceWalk, etc.).

  2. Have RandomWalk contain a "WalkRandomly" function which takes in a Positional parameter. Entities which need need random movement can call this function. If necessary, an abstract class ICanRandomWalk could be inherited from (diamond inheritance is fine if no more than one base class contains an implementation), with the ICanRandomWalk being implemented as calling a member of a private instance of RandomWalk.

answered May 13, 2015 at 18:34
2
  • Thank you. Your second point seems like a simple solution. Having to manually pass in the Position seems excessive though; is there a clean way around that? Is that what you're referring to with ICanRandomWalk? Have it as a ABC that requires the inheritor to implement a method that passes in the Position? Again, thanks for your suggestions. Commented May 13, 2015 at 19:19
  • @Carcigenicate: Yes, my interface proposal is intended to allow external callers to tell any implementor of ICanRandomWalk to randomly walk. I admit this is slightly annoying in that every implementer of ICanRandomWalk must copy/paste an identical implementation of the randomwalk function. You could instead pass position to RandomWalk's constructor, though this means RandomWalk contains state information. Commented May 13, 2015 at 20:38

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.