Why no virtual methods

Hi guys. I was wondering if a more experienced C++ programmer could explain to me why Box2D uses so many switch statements instead of virtual methods. A typical example is b2Shape::Create.
Do virtual methods not work on some platforms, or is it just a performance issue, or am I missing something?

I ask as I am looking at porting my controller code. Unlike shapes, I don't expect a fixed list of controllers, so it's preferable to encapsulate each controller.
From my AS3 background, virtual methods are the natural approach, but this seems against the Box2D paradigm for some reason.

Don't be afraid to point out the obvious; I am an amateur programmer, and I'm sure my knowledge is very patchy.
Performance I guess. Virtual functions are expensive if called a lot.

Might also be a matter of style, virtual functions are not without problems (oops I forgot to make the destructor virtual and now my code blows up)
Daid wrote:
Performance I guess. Virtual functions are expensive if called a lot.

Is it actually faster when you're still doing the lookup yourself, though? (which is essentially what happens in a switch like that) It may be, I'm not too familiar with C++ performance issues, but it seems to me that the vtable lookup shouldn't usually cost a whole lot more than a switch that's essentially checking a list of the subclasses anyways. Is that wrong?

I suspect that it's probably just done that way because it was more straightforward - in a shape construction function speed is not the primary concern, since you're not calling it every frame or anything like that.

In the case of the b2Shape::Create method, though, I would think that it could reasonably be replaced by a set of Create methods for each shape type, which would avoid requiring a cast in user code, e.g.
Code:
b2CircleShape* b2Shape::Create(const b2CircleDef* def, b2BlockAllocator* allocator);
b2PolygonShape* b2Shape::Create(const b2PolygonDef* def, b2BlockAllocator* allocator);

instead of
Code:
b2Shape* b2Shape::Create(const b2ShapeDef* def, b2BlockAllocator* allocator);

...though to avoid a cast, you'd also need to add methods per shape type to the b2Body class, so it would be more code in several places for just a minor gain in convenience.
Let's see. The user has a shape definition and wants to create a shape. Right now they have to create a shape using a body. There is a factory method b2Body::CreateShape. I could overload this for each type of shape, but this would clutter the b2Body interface. Also, you may want to store an array of b2ShapeDef pointers. You would have to down-cast them. It would not help to make b2Body::CreateShape virtual because nothing derives from b2Body.

Now b2Body::CreateShape calls b2Shape::CreateShape. Again this is a factory method that cannot be virtual because it is static. There is no shape pointer object yet.

So I'm curious how you would implement a factory with virtual methods instead of branching.
Well, in this particular example, I'd define it as follows:

virtual b2Shape* b2ShapeDef::CreateShape(b2Allocator *allocator);

b2Shape* b2Body::CreateShape(b2ShapeDef &shapeDef)
{
b2Shape *shape = shapeDef.CreateShape(m_world.GetAllocator());
//...
return shape;
}

I.e. each shape def is responsible for creating it's own shape, by overriding CreateShape. There is no overloading.
I can see that this is putting allocation burdens on each individual shape class, which is not pleasant. But I think that is just for this specific example, (as allocation is always going to cross-cut proper object encapsulation). Similarly for the intermixing between the def and the object itself. If I'd have picked a better example, like say debug drawing, then it would an even neater way of doing it.
Uh, now I look closer, constructors, destructors and debug drawing are the only examples of switch statements that could be virtualized which haven't been. So I'm probably making much ado about nothing.

Original thread: https://box2d.org/forum/viewtopic.php?t=1639