I have the following class,
class Simulation {
public:
btDefaultCollisionConfiguration collisionConfiguration;
btBroadphaseInterface broadphase;
btSequentialImpulseConstraintSolver solver;
btDynamicsWorld dynamicsWorld;
Simulation() {
broadphase = btCollisionDispatcher(&collisionConfiguration);
}
}
and when I compile I get this error
cannot declare field ‘Simulation::broadphase’ to be of abstract type ‘btBroadphaseInterface’
Now I think I know what I did wrong. If this compiled then the moment I called the constructor the memory allocated would be for a virtual class and wouldn’t contain the subclass(btCollisionDispatcher). I know how to solve this with pointers, but I’ve been hearing from people that you should advoid pointers in C++ and I thought I would give it a try. I’ve been successful so far but I don’t know what to do here.
Yeah, you can only have pointers or references to abstract classes because you can’t instantiate an abstract class because, by definition, it has an “incomplete” definition (not to be taken literally). It’s just an interface.
If you want to avoid pointers, you can use references, like this:
Or smart pointers, like this: