Let's say I have an interface called
ParantClass has two implementations,
ParentClassB. There is also the
ChildClass interface, with a
ChildClassB implementation. The
ParentClassinterface has a function called
createChild, which returns a pointer to a
ChildClass interface of the appropriate type (
ParentClassA::createChild returns a
ParentClassB::createChild returns a
The code tends to look like this:
ChildClass *child = parentClass->createChild(); child->destroy();
The inefficiency here is that the
ParentClass interface will only ever return a
ChildClass interface of the same implementation, so there's a ton of vtables being used but there's only one implementation that they lead to.
A theoretically more efficient solution would be for
ParentClass::createChild to return an opaque pointer, and to have all member functions contained in
ChildClass to be moved to
ParentClass and have the opaque pointer be passed as the first argument. This will result in only a single vtable, but the code ends up being a bit uglier.
And that code tends to look like this:
ChildClass *child = parentClass->createChild(); parentClass->destroyChild(child);
This part of my application is used quite frequently, so performance is an important consideration. But readability and maintainable code is as well. I'm not sure which approach I should be using.