Let's say that I have 4 classes, B,C,D,E
that inherit from A
(abstract base class).
Also I have a container (std::vector
) of type A*
whose contents point to either of
B,C,D,E
objects.
Here are some rules:
If a B
object and a C
object interact, they get removed from the vector and in their place a D
object gets created.
Also, C + D = E
Now, suppose that I randomly choose one of said vector contents; how would I go about knowing which object is of which type, in order to implement an interaction mechanic?
NOTE: I do not wish to use the typeid
operator, a dynamic cast or flags. Any other solutions?
Here is some code
#include <iostream>
class A {
protected:
int v;
public:
A(){}
~A(){}
};
class B :public A {
public:
B(){}
~B(){}
};
class C : public A {
public:
C(){}
~C(){}
};
class D : public A {
public:
D(){}
~D(){}
};
class E : public A {
public:
E(){}
~E(){}
};
int main()
{
std::vector<A*> container;
return 0;
}
How would I implement the interact function(s) ?
You may use virtual functions to do the multiple dispatch
and then