Direct versus virtual call of a virtual function

I am self-taught, and therefore I am not familiar with great terminology. It seems that I can not find the answer to this question by the search query: what is a "virtual" and a "direct" call to a virtual function?

This refers to terminology, not technical. I ask when the call is defined as “direct” and “actually”. This does not apply to vtables or anything else related to the implementation of these concepts.

+4
source share
2 answers

The answer to your question is different at different conceptual levels.

  • " " , , . ++, , , . , " "

    SomeObject obj;
    SomeObject *pobj = &obj;
    SomeObject &robj = obj;
    
    obj.some_virtual_function(); // Virtual call
    pobj->some_virtual_function(); // Virtual call
    robj.some_virtual_function(); // Virtual call
    
    obj.SomeObject::some_virtual_function(); // Direct call
    pobj->SomeObject::some_virtual_function(); // Direct call
    robj.SomeObject::some_virtual_function(); // Direct call
    

    , , , , , " ". . , : . [] .

  • " " , , , . (VMT), , . VMT , , .. . , "" .

    , , ( ), ( VMT). ,

    SomeObject obj;
    SomeObject *pobj = &obj;
    SomeObject &robj = obj;
    
    obj.some_virtual_function(); // Direct call
    pobj->some_virtual_function(); // Virtual call in general case
    robj.some_virtual_function(); // Virtual call in general case
    
    obj.SomeObject::some_virtual_function(); // Direct call
    pobj->SomeObject::some_virtual_function(); // Direct call
    robj.SomeObject::some_virtual_function(); // Direct call
    

    , ( VMT), . .

+8

, :

class X { 
public: 
    virtual void myfunc(); 
};  

X, , X::myfunct():

X a;         // object of known type 
a.myfunc();  // will call X::myfunc() directly    

, , , . X, , X. , .. :

X *pa;        // pointer to a polymorphic object  
...           // initialise the pointer to point to an X or a derived class from X
pa->myfunc(); // will call the myfunc() that is related to the real type of object pointed to    

- . , , - (.. "" -wired " ).

+5

All Articles