views:

61

answers:

2

I have a class hierarchy and want to get rid of virtual method calls overhead using CRTP pattern. How to do this for my example classes, is it doable ?

class A
{
public:
    virtual ~A();
    virtual void foo();
};

class B : public A
{
public:
    virtual ~B();
    virtual void foo();
};

class C : public B
{
public:
    virtual ~C();
    virtual void foo();
};
A: 

It depends on where and how you intend to use your instances of type A, B and C. For instance, if you add a template parameter to all your methods, where you can provide the type of the object, then simply removing all "virtual" keywords in your code (and maybe even the inheritance to A and B) will do the trick.

Benoît
+1  A: 

Since you say you're trying to get rid of the virtual function overhead, I assume that you're worried about performance. Have you actually profiled your application and determined that making these virtual calls is a significant proportion of time spent? I personally haven't observed cases where virtual call overhead wasn't completely dwarfed by other portions of the program, such as linear or worse algorithmic complexity in other code.

Mark B