virtual functions understanding



  • #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.
    

  • Moderators

    @guru007

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


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


  • Moderators

    @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
    }
    


  • 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.


  • Moderators

    @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.



  • 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.



  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.