I am writing several related C ++ classes and I am having some problems designing a specific inherited function.
In particular, classes are all “operations” on trees, and I need to be able to perform a set of arbitrary tree actions on the tree. I have a function called ExecuteOperation () in each class.
The problem is that in some classes I need more information than in others. Right now, I'm just passing extra information to all classes, with a base base class defined as follows:
class BasicOperation { public:
The descendants of the BasicOperation subclass, "SpecialOperation", need the extra1 and extra2 parameters, and the descendants of the "NonspecialOperation" don't use these parameters at all.
An ExecuteOperation subscriber usually looks something like this:
Tree* tree; std::vector<BasicOperation*> operations;
Filling extra1 and extra2 depends on a particular tree and can be intensive computation (it is better not to recreate them inside each call to ExecuteOperation).
Is there a better way to design this so that only SpecialOperation objects receive the parameters passed to them? Or is it usually just fine to use unused parameters for classes like NonspecialOperation?
The definition of NonspecialOperation :: ExecuteOperation () with two parameters that it does not use is strange.
The only thing I've been thinking so far is to provide SpecialOperation objects with each pointer back to the caller and store additional information in the caller's object. I do not really like this solution, because extra1 and extra2 are too dependent on the current state; problems with parallelization that the installation simply “feels” wrong.
In addition, wrapping the tree, extra1 and extra2 in another structure to go into ExecuteOperation can make it cleaner and makes sense, because extra1 and extra2 are additional descriptors for the tree, but I don't know if this is the best solution
struct TreeEx { Tree* tree; multimap<int,Foo*> extra1, extra2; };