Possible Duplicate:
Virtual functions and performance – C++
I have some class:
class I
{
public:
virtual void foo() = 0;
protected:
virtual ~I(){}
};
This class does not provide interface for instance deletion, so making destructor protected is quite logical solution. For this reason it is unnecessary to make the destructor virtual. But I have code where it is made virtual.
It doesn’t look like a big mistake, but is it significantly for the code perfomance?
When we create virtual function we add one more record to the virtual function table and when we make virtual call we search in this table. So it means that looking up time increases. Am I right?
Even if this were an issue (which it isn’t), no, it wouldn’t add time. Lookup isn’t the runtime actually looking up anything – it doesn’t matter how big the vftable is. It knows exactly where to jump within the table.
So – there is some cost to calling a
virtualmethod – there’s no cost to the actual look-up.