Wednesday, November 26, 2003

Re: Polymorphism

" It will look to see if area() is a virtual function. If it is, it will look to see what slot the address of area() appears in Shape's vtable. Once it figures that out, it will go to Circle's vtable, find the same slot and dispatch the call to the address that is found there. "

are u sure about this? ... cause i always thought that the pointer created on the object directly points to the v-table of the class that is if i were to write

class Base {
virtual void pr() { cout << "Base" << endl;}

class Derv : public Base {
void pr() { cout << "Derived" << endl; }

and if i were to write in main()

Base* ptr;
ptr = new Derv();

the object created on the free store would have a pointer to Derv's v-table directly .. so why does it have to look at Base's v-table? for the index?

wouldn't it be a major run-time overhead if u had to go thru the base everytime u called a virtual function ... if any thing implementations say that the virtual function does not cause any major function mechanism overheads other than that pointer on the object and the v-table itself for each polymorphic class

just check it out ... i could not find enough info about the actual implementation of the v-table.


No comments: