Neil, I understand completely what youare saying. However, when you are calling a virtual function of a class through its base class pointer, I do not believe that the compiler can resolve the function call. Hence, it requires a runtime lookup.
Consider:
class a{virtual int foo(){return 1;}}; class b : public a {virtual int foo() {return 2;}};
class a* pa1, pa2; class b* pb1; int res1, res2, res3; pa1 = new a; pb1 = new b; pa2 = pb1;
res1 = pa1->foo(); res2 = pa2->foo(); res3 = pb1->foo();
The results are res1 = 1, res 2 = 2, and res3 = 2. However, unless I am seriously mistaken (of which I consider the possibility), res2 requires a runtime determination as to which foo to call (aka which vtable to dereference). The compiler has no way of knowing if pa2 (or pa1 for that matter) will contain a base class or a derived class at runtime. All C++ code can not be resolved at compile time, there is some runtime overhead. That is a fact of which I am quite sure. |