GameProgrammerDude
Programmer
Does anyone know a way I could implement the following functionality?
class CollidePrim
{
// blah
}
class Sphere : public CollidePrim
{
// blah
}
class Triangle : public CollidePrim
{
// blah
}
class PhysicsObject
{
CollidePrim *m_pCollidePrim;
void SetPrimitive();
void Update();
void Collide( Sphere& sS );
void Collide( Triangle& tT );
}
void PhysicsObject::SetPrimitive()
{
if( externaldata(Prim) == "Sphere" )
m_pCollidePrim = new Sphere;
else
m_pCollidePrim = new Triangle;
}
PhysicsObject::Update()
{
Collide( *m_pCollidePrim );
}
This code will not compile, but I think it is clear what I am trying to do. I want the compiler to choose which collide function depending on the collision primitive that is embedded in class PhysicsObject. And I would like to avoid using an abstract base class, because I don't want the overhead of a vtable. Anyway, I hope this is clear enough of an explanation.
Thanks.
class CollidePrim
{
// blah
}
class Sphere : public CollidePrim
{
// blah
}
class Triangle : public CollidePrim
{
// blah
}
class PhysicsObject
{
CollidePrim *m_pCollidePrim;
void SetPrimitive();
void Update();
void Collide( Sphere& sS );
void Collide( Triangle& tT );
}
void PhysicsObject::SetPrimitive()
{
if( externaldata(Prim) == "Sphere" )
m_pCollidePrim = new Sphere;
else
m_pCollidePrim = new Triangle;
}
PhysicsObject::Update()
{
Collide( *m_pCollidePrim );
}
This code will not compile, but I think it is clear what I am trying to do. I want the compiler to choose which collide function depending on the collision primitive that is embedded in class PhysicsObject. And I would like to avoid using an abstract base class, because I don't want the overhead of a vtable. Anyway, I hope this is clear enough of an explanation.
Thanks.