So.
I am working on a small game project for school and I got collision and stuff working for my objects, and the idea is to check if an object collides with another and then have the specific logic for each type have a bunch of overloaded functions depending on the object being sent in. For instance, if the object is a player-controlled object, enemies will hurt it, but if it is a powerup colliding with an enemy, things will be fine.
So, I was hoping I could do something like (everything inherits from Obj obviously):
std::vector<Obj*> list;
list.push_back(new Powerup());
list.push_back(new Enemy());
list.push_back(new Player());
for (auto i: list) {
for (auto j: list) {
if (collision(i,j)) {
i->doStuff(*j);
}
}
}
But I'm having trouble finding a way to send in the proper type. I made a test program demonstrating the problem:
#include <iostream>
class A {
public:
virtual void doStuff(A& T) { std::cout << "A->A!" << std::endl; }
};
class B : public A {
public:
virtual void doStuff(A& T) { std::cout << "B->A!" << std::endl; }
};
class C : public A {
public:
virtual void doStuff(A& T) { std::cout << "C->A!" << std::endl; }
virtual void doStuff(B& T) { std::cout << "C->B!" << std::endl; }
};
int main() {
A* base;
A a;
B b;
C c;
c.doStuff(a);
c.doStuff(b);
base = &a;
c.doStuff(*base);
base = &b;
c.doStuff(*base);
return 0;
}
And running it I get this:
C->A!
C->B!
C->A!
C->A!
When I was expecting:
C->A!
C->B!
C->A!
C->B!
Any idea how to make this work?