can anybody explain me vtable and related things using this code snippet?
-
class A { public: void print() { printf("print A"); } virtual void write() { printf("write A"); } }; class B: public A { public: void print() { printf(" print B"); } virtual void write() { printf("write B"); std::cout<<"try"; } }; int main() { // A *pA = new A; // pA->print(); // pA->write(); A *pB = new B; pB->print(); pB->write(); return 0; }
Thanks & Regards rakesh baldha
-
class A { public: void print() { printf("print A"); } virtual void write() { printf("write A"); } }; class B: public A { public: void print() { printf(" print B"); } virtual void write() { printf("write B"); std::cout<<"try"; } }; int main() { // A *pA = new A; // pA->print(); // pA->write(); A *pB = new B; pB->print(); pB->write(); return 0; }
Thanks & Regards rakesh baldha
A virtual table is a table containing the addresses of all the virtual functions of the class that was instancied (so that the call can be redirected to the correct derived class function). In your example, print was not declared as virtual so it is not in the vtable but write is virtual (so it is in the vtable). So in this statement:
A *pB = new B;
pB was declared as a pointer to an object A and was instancied as a pointer to an object B. So, the virtual table contain one virtual function (the write function) that is redirected to the write function of the B class. For the compiler pB has been declared as a pointer to A so if you call print, it will call print from the class A (the call is not redirected because the function is not in the vtable). When you call write, the call will be redirected to write of the B class because of the vtable. I hope this is clear...
Cédric Moonen Software developer
Charting control [Updated - v1.1] -
A virtual table is a table containing the addresses of all the virtual functions of the class that was instancied (so that the call can be redirected to the correct derived class function). In your example, print was not declared as virtual so it is not in the vtable but write is virtual (so it is in the vtable). So in this statement:
A *pB = new B;
pB was declared as a pointer to an object A and was instancied as a pointer to an object B. So, the virtual table contain one virtual function (the write function) that is redirected to the write function of the B class. For the compiler pB has been declared as a pointer to A so if you call print, it will call print from the class A (the call is not redirected because the function is not in the vtable). When you call write, the call will be redirected to write of the B class because of the vtable. I hope this is clear...
Cédric Moonen Software developer
Charting control [Updated - v1.1]thank you very much.
Thanks & Regards rakesh baldha
-
thank you very much.
Thanks & Regards rakesh baldha
You're welcome. Please next time use the pre or code tags and indent your code correctly so it will be more readable.
Cédric Moonen Software developer
Charting control [Updated - v1.1]