I'm thinking about a proper class design for the following case: Let's say I want to create a class design which resembles (mathematical) functions. All functions derive from base class Function
. Now one could think of a function FuncB
which is the sum of multiple objects of FuncA
. In my case FuncB
and FuncA
are rather complicate objects but my current implementation boils down to:
class Function
{
virtual double value() = 0;
};
class FuncA : public Function
{
public:
virtual double value() {return v;}
private:
double v;
};
class FuncB : public Function
{
virtual double value() {
double result = 0;
for( auto i : v)
result += i.value();
return result;
}
private:
std::vector<DerivedA> v;
};
Somehow I would consider this as a bad class design since FuncB
has a member which inherits from the same base class as the class itself. Any suggestions how to solve this more elegant?
The composite design pattern
It allows you to create a drawing system - where a composite object of different shapes can be treated as an agregate
It allows you to create a simple script of things to do for do/undo logic.