I need to save a container of pointers to objects. These objects have some common methods / attributes (interface) that I want to use (possibly at compile time) and use. Example:
struct A{
void fly(){}
};
struct B{
void fly(){}
};
A a;
B b;
std::vector<some *> objects;
objects.push_back(&a);
objects.push_back(&b);
for(auto & el: objects)
el->fly();
A simpler solution would be A, and Binherit a common base class, for example FlyingClass:
struct FlyingClass{
void fly(){}
};
struct A: public FlyingClass { ...
struct B: public FlyingClass { ...
and create
std::vector<FlyingClass *> objects;
This will work, as well as ensure the fact that I can only add objectsthings that can fly (implement FlyingClass).
But what if I need to implement some other common methods / attributes WITHOUT binding them to the above base class?
Example:
struct A{
void fly(){}
void swim(){}
};
struct B{
void fly(){}
void swim(){}
};
And I would like to:
for(auto & el: objects) {
el->fly();
...
el->swim();
...
}
, , , /, :
void dostuff(Element * el){
el->fly();
el->swim();
}
, :
struct SwimmingClass{
void swim(){}
};
struct A: public FlyingClass, public SwimmingClass { ...
struct B: public FlyingClass, public SwimmingClass { ...
?
std::vector<FlyingClass&&SwimmingClass *> objects;
, SwimmingFlyingClass, , RunningClass .. .
, , ?
- ?
, .