Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Dynamic Dispatch

Dynamic Dispatch

Scheduled Pinned Locked Moved C / C++ / MFC
oopquestionlearning
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hrishiS
    wrote on last edited by
    #1

    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

    C H S 3 Replies Last reply
    0
    • H hrishiS

      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

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      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++

      S 1 Reply Last reply
      0
      • H hrishiS

        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

        H Offline
        H Offline
        hema_soft
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • C Cedric Moonen

          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++

          S Offline
          S Offline
          Stuart Dootson
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • H hrishiS

            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

            S Offline
            S Offline
            Stuart Dootson
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups