Avoiding virtual method calls

I was about to write a post about how to avoid virtual method calls in C++.

While looking for the correct wording for one of the lines I stumbled across this:
Getting rid of virtual method calls

Since my explanation wasn’t much better,
I’ll just leave you with the link to Floh’s article
and a snippet of our rendering engine.

[renderer.hpp]
 
#if defined( OM_PLATFORM_IPHONE )
	#include "iphone/renderer_iph.hpp"
	namespace OM
	{
		#define Renderer OM::Renderer_Iph
	}
#elif defined( OM_PLATFORM_OSX )
	#include "osx/renderer_osx.hpp"
	namespace OM
	{
		#define Renderer OM::Renderer_Osx
	}
[...]
[renderer_osx.hpp]
namespace OM
{
	class Renderer_Osx : public RendererBase
	{
		public:
[...]

Just one more thing to say:
This works perfectly for us on the 9 supported platforms.

Popularity: 44% [?]

Tags:

2 Responses to “Avoiding virtual method calls”

  1. tonic says on :

    So how do you handle the case when the same build should support two different renderers?

    For example, GLES 1.1 for older iPhones and 2.0 for newer ones.

    Well, specifically in iPhone case the build can already be separated by the arm architecture, which allows #ifdef-selection of the renderer as well.. but still, generically, is there any other tricks I’m not aware of which than to have separate binaries of “whole app” if there’s need for two renderers but still optimizing virtual calls away?

  2. admin says on :

    I would love to give you a long and detailed answer,
    but the answer (at this time) is:

    We don’t.
    Our engine supports iPhone/iPod touch,
    but so far only has a GLES 1.1 render path.

    I guess our solution will be very pragmatic.

    Step 1)
    Support/Use only GLES 1.1

    Step 2)
    Have to different Apps.
    (One GLES 1.1 and one GLES 2.0)

    Step 3)
    Support only GLES 2.0.
    (I believe the usage time of old apple equipment is pretty short, so GLES 1.1 won’t be around for too long.)