Dynamic Dispatch
-
Hi to All, Could you please clear my following doubt. I have 3 class with multilevel inheritance as follows,,... class A { public: A() { s(); } virtual void s() { printf("\nIn A"); } }; class B:public A { public: }; class C:public B { public: void s() { printf("\nIn C"); } }; int main(int argc, char* argv[]) { A *ptr = new C(); } when i create an object of C,...by theory it should call always the method S of C....am I correct?...but when I called C from A's constructor Its calling S() of C....Why so?...Could anyone please explain me the reason and some points on it?? thanks in advance
----------------------------- I am a beginner
-
Hi to All, Could you please clear my following doubt. I have 3 class with multilevel inheritance as follows,,... class A { public: A() { s(); } virtual void s() { printf("\nIn A"); } }; class B:public A { public: }; class C:public B { public: void s() { printf("\nIn C"); } }; int main(int argc, char* argv[]) { A *ptr = new C(); } when i create an object of C,...by theory it should call always the method S of C....am I correct?...but when I called C from A's constructor Its calling S() of C....Why so?...Could anyone please explain me the reason and some points on it?? thanks in advance
----------------------------- I am a beginner
hrishiS wrote:
but when I called C from A's constructor Its calling S() of C....
You mean it is calling s() of A, right ? That's normal because when A is being constructed, it's virtual table is not create yet, which means that the call can't be redirected properly. So, the rule is simple: never call a virtual function from within a constructor. Read this[^] for instance.
Cédric Moonen Software developer
Charting control [v2.0] OpenGL game tutorial in C++ -
Hi to All, Could you please clear my following doubt. I have 3 class with multilevel inheritance as follows,,... class A { public: A() { s(); } virtual void s() { printf("\nIn A"); } }; class B:public A { public: }; class C:public B { public: void s() { printf("\nIn C"); } }; int main(int argc, char* argv[]) { A *ptr = new C(); } when i create an object of C,...by theory it should call always the method S of C....am I correct?...but when I called C from A's constructor Its calling S() of C....Why so?...Could anyone please explain me the reason and some points on it?? thanks in advance
----------------------------- I am a beginner
When u create base class pointer as derived class using new, it first allocate memory for base class one by one, call their constructors, initialize their member variables , finally it call declared class constructor(here it is Class C). since fn s() is called in class A's Constructor, it invoke its own fn even though u declared it as virtual. A *ptr = new C(); if you define like this , A *ptr = new C(); ptr->s(); Class C's fn of s() will be invoked!
hemmalatha.g
-
hrishiS wrote:
but when I called C from A's constructor Its calling S() of C....
You mean it is calling s() of A, right ? That's normal because when A is being constructed, it's virtual table is not create yet, which means that the call can't be redirected properly. So, the rule is simple: never call a virtual function from within a constructor. Read this[^] for instance.
Cédric Moonen Software developer
Charting control [v2.0] OpenGL game tutorial in C++Not quite, Cedric - in A's constructor, the object has A's v-table. See the C++ 98 Standard, Section 12.7-3.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
Hi to All, Could you please clear my following doubt. I have 3 class with multilevel inheritance as follows,,... class A { public: A() { s(); } virtual void s() { printf("\nIn A"); } }; class B:public A { public: }; class C:public B { public: void s() { printf("\nIn C"); } }; int main(int argc, char* argv[]) { A *ptr = new C(); } when i create an object of C,...by theory it should call always the method S of C....am I correct?...but when I called C from A's constructor Its calling S() of C....Why so?...Could anyone please explain me the reason and some points on it?? thanks in advance
----------------------------- I am a beginner
When constructing an object, base class constructors use that base classes virtual function table. So, when you call C's constructor, the first thing it does is call B's (implicitly defined) constructor, which call's A's constructor. Within A's constructor, only virtual functions defined in A can be called. Within B's constructor, only virtual functions defined in A or B can be called. Within C's constructor, virtual functions defined in A, B or C could be called.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p