Object Handling
I want to have a list of objects that are linked by their base class. So I
will have a class that manages all the different flavors of objects but
are uniformly stored in their base class form. I want to be able to handle
a test collision between two objects one being a sphere and the other a
cube. I would say call a function like so sphere->collisionTest(cube).
Using polymorphism I can call the collisionTest method and it will go to
appropriate method in the sphere class but I need a way to
identify/override which collisionTest method within the sphere class.
Parameter overriding a sphere.collisionTest(sphere) or
sphere.collisionTest(cube). Here is some example code of what I have
tried. Here when I call the collisionTest(object->getObject()) method I
believe I should be getting the cube object and then it will parameter
override and call the collisionTest(Cuboid *object) method. Instead it
calls the same collisionTest(Geom *object).
int main()
{
Geom *geomHandler1;// = new Geom();
Geom *geomHandler2;// = new Geom();
Sphere *sphereGeom = new Sphere();
Cuboid *cuboidGeom = new Cuboid();
geomHandler1 = (Geom*)sphereGeom;
geomHandler2 = (Geom*)cuboidGeom;
geomHandler1->collisionTest(geomHandler2);
return 0;
}
#include "Sphere.h"
void Sphere::collisionTest(Geom *object)
{
collisionTest(object->getObject());
}
void Sphere::collisionTest(Cuboid *object)
{
}
Sphere* Sphere::getObject()
{
Sphere *husk = new Sphere();
return husk;
}
#include "Cuboid.h"
void Cuboid::collisionTest(Geom *object)
{
}
void Cuboid::collisionTest(Sphere *object)
{
}
Cuboid* Cuboid::getObject()
{
Cuboid *husk = new Cuboid();
return husk;
}
No comments:
Post a Comment