Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. virtual functions understanding
QtWS25 Last Chance

virtual functions understanding

Scheduled Pinned Locked Moved Unsolved C++ Gurus
8 Posts 3 Posters 3.9k Views
  • 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.
  • G Offline
    G Offline
    guru007
    wrote on 3 Aug 2017, 10:00 last edited by
    #1
    #include <iostream>
    
    using namespace std;
    class A{
    public:
        virtual void x()
        {
            cout << "Class A";
        }
        virtual void y()
        {
            cout << "Class A";
        }
    
    };
    class B {
    public:
        virtual void y()
        {
            cout << "Class B";
        }
    };
    class C : public A , public B
    {
        virtual void y()
        {
            cout << "Class C";
        }
    };
    
    int main(int argc, char *argv[])
    {
        cout << "Hello World!" << endl;
        C *ptr = new C;
        ptr->y();
        return 0;
    }
    
    This ptr is pointing to y() function in C. How I can Call y() function of B class.
    
    R 1 Reply Last reply 3 Aug 2017, 10:12
    0
    • G guru007
      3 Aug 2017, 10:00
      #include <iostream>
      
      using namespace std;
      class A{
      public:
          virtual void x()
          {
              cout << "Class A";
          }
          virtual void y()
          {
              cout << "Class A";
          }
      
      };
      class B {
      public:
          virtual void y()
          {
              cout << "Class B";
          }
      };
      class C : public A , public B
      {
          virtual void y()
          {
              cout << "Class C";
          }
      };
      
      int main(int argc, char *argv[])
      {
          cout << "Hello World!" << endl;
          C *ptr = new C;
          ptr->y();
          return 0;
      }
      
      This ptr is pointing to y() function in C. How I can Call y() function of B class.
      
      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 3 Aug 2017, 10:12 last edited by raven-worx 8 Mar 2017, 10:12
      #2

      @guru007

      C *ptr = new C;
      ptr->B::y();
      

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      2
      • G Offline
        G Offline
        guru007
        wrote on 3 Aug 2017, 10:40 last edited by
        #3

        Can you please explain How V Table will work in this example

        R 1 Reply Last reply 3 Aug 2017, 10:51
        0
        • G guru007
          3 Aug 2017, 10:40

          Can you please explain How V Table will work in this example

          R Offline
          R Offline
          raven-worx
          Moderators
          wrote on 3 Aug 2017, 10:51 last edited by raven-worx 8 Mar 2017, 10:53
          #4

          @guru007
          what do you mean exactly?
          In my example the method is explicitly addressed by the classname. If left out the "latest" / "most derived" version of the method is chosen.
          It's basically the same like calling a base-class implementation from within a method:

          void B::myVirtualMethod()
          {
                A::myvirtualMethod(); // call base class implementation
          }
          

          is the same as:

          void B::myVirtualMethod()
          {
                this->A::myvirtualMethod(); // call base class implementation
          }
          

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • G Offline
            G Offline
            guru007
            wrote on 3 Aug 2017, 11:00 last edited by guru007 8 Mar 2017, 11:06
            #5

            I got your point. But I want to understand more about vtable;

            class A{
            public:
                virtual void y()
                {
                    cout << "Class A";
                }
            
            };
            class B: virtual public A 
            {
            public:
                virtual void y()
                {
                    cout << "Class B";
                }
            };
            class C : virtual public A 
            {
                virtual void y()
                {
                    cout << "Class C";
                }
            };
            class D : public B, public C
            {
                virtual void y()
                {
                    cout << "Class D";
                }
            };
            
            int main()
            {
            A *ptr = new D;
            ptr->y();
            }
            
            my question is How many virtual tables class D will contain.
            
            Also If we remove virtual keyword in:
            

            class C : virtual public A
            then It creates ambiguity. How it works internally.

            R 1 Reply Last reply 3 Aug 2017, 11:05
            0
            • G guru007
              3 Aug 2017, 11:00

              I got your point. But I want to understand more about vtable;

              class A{
              public:
                  virtual void y()
                  {
                      cout << "Class A";
                  }
              
              };
              class B: virtual public A 
              {
              public:
                  virtual void y()
                  {
                      cout << "Class B";
                  }
              };
              class C : virtual public A 
              {
                  virtual void y()
                  {
                      cout << "Class C";
                  }
              };
              class D : public B, public C
              {
                  virtual void y()
                  {
                      cout << "Class D";
                  }
              };
              
              int main()
              {
              A *ptr = new D;
              ptr->y();
              }
              
              my question is How many virtual tables class D will contain.
              
              Also If we remove virtual keyword in:
              

              class C : virtual public A
              then It creates ambiguity. How it works internally.

              R Offline
              R Offline
              raven-worx
              Moderators
              wrote on 3 Aug 2017, 11:05 last edited by raven-worx 8 Mar 2017, 11:05
              #6

              @guru007
              every class has it's own (shared) vtable and a reference to the inherited base class vtable.
              See this for example, to get a better understanding.

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              3
              • G Offline
                G Offline
                guru007
                wrote on 3 Aug 2017, 11:20 last edited by
                #7

                Referring from link:
                Also as a reminder, any class that uses virtual functions has a __vptr, and thus each object of that class will be bigger by one pointer. Virtual functions are powerful, but they do have a performance cost.

                It means class D instance contains Four virtual tables because all classes using virtual functions.

                1 Reply Last reply
                0
                • Q Offline
                  Q Offline
                  Qtakshay
                  wrote on 7 Aug 2017, 10:20 last edited by Qtakshay 8 Jul 2017, 12:03
                  #8

                  Hi,

                  If we remove virtual keyword in:

                  class C : virtual public A
                  then It creates ambiguity.

                  How it works internally.

                  Class D is derived from both Class B and Class C , in which both Class B and Class C are derived from same class i.e Class A ,
                  so that's the reason ambiguity is created ,
                  when you remove virtual keyboard in Class C,

                  Thanks
                  Akshay.

                  1 Reply Last reply
                  0

                  1/8

                  3 Aug 2017, 10:00

                  • Login

                  • Login or register to search.
                  1 out of 8
                  • First post
                    1/8
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved