SI
SI
discoversearch

We've detected that you're using an ad content blocking browser plug-in or feature. Ads provide a critical source of revenue to the continued operation of Silicon Investor.  We ask that you disable ad blocking while on Silicon Investor in the best interests of our community.  If you are not using an ad blocker but are still receiving this message, make sure your browser's tracking protection is set to the 'standard' level.
Technology Stocks : BORL: Time to BUY! -- Ignore unavailable to you. Want to Upgrade?


To: Neil Booth who wrote (8335)12/31/1997 3:05:00 AM
From: Kashish King  Respond to of 10836
 
The key is that an object's vtbl is a sub-sequence of any subclass' vtbl:

( * (pObject->vtbl [ 2 ] ) ) ( )

No matter where in the class hierarchy the object pointed to by pObject exists, the vtbl offsets will be identical. In other words, if the root class has two virtual functions then all vtbls of all derived classes will also have at least two vtbl slots and offsets 1 and 2 will always point to those functions respectively.



To: Neil Booth who wrote (8335)12/31/1997 12:00:00 PM
From: David R  Read Replies (2) | Respond to of 10836
 
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.