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. can anybody explain me vtable and related things using this code snippet?

can anybody explain me vtable and related things using this code snippet?

Scheduled Pinned Locked Moved C / C++ / MFC
question
4 Posts 2 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.
  • B Offline
    B Offline
    baldha rakesh
    wrote on last edited by
    #1

    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

    C 1 Reply Last reply
    0
    • B baldha rakesh

      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

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

      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]

      B 1 Reply Last reply
      0
      • C Cedric Moonen

        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]

        B Offline
        B Offline
        baldha rakesh
        wrote on last edited by
        #3

        thank you very much.

        Thanks & Regards rakesh baldha

        C 1 Reply Last reply
        0
        • B baldha rakesh

          thank you very much.

          Thanks & Regards rakesh baldha

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

          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]

          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