using-declaration in derived class does not hide same function derived from base class

Liu Nick

Have a look at the following code:

struct A {
public:
    virtual void f(){std::cout << "in A";};
};

struct B : A{
public:
   virtual void f(){std::cout << "in B";};
   int a;
};

struct C : B{
    using A::f;
    void test(){f();}
};


int main() 
{
    C c;
    c.f(); // calls B::f, the final overrider
    c.C::f(); // calls A::f because of the using-declaration
    c.test(); //calls B::f
    return 0;
}

Per my understanding, the B::f() in C should hide the A::f() which is brought to C by using-declaration; if so, then why does c.C::f() still call A::f()?

If c.C::f() calls A::f(), that should mean that in the scope of C, f() should be always refer to A::f(), this is the function of the using-declaration. Then why in the C::test(), call to f() is still evaluated to B::f()?

Angew is no longer proud of SO

Very nice question, a complicated case of name lookup.

Basically, when the name f is looked up in the scope of C, it always finds A::f due to the using-declaration. So all the calls c.f(), c.C::f(), and f() in C::test(), resolve the name f to A::f.

Next comes virtual dispatch. If a virtual function is called by an unqualified name, dynamic dispatch happens and the final overrider is called. This covers c.f() and the f() call in C::test(), since these are unqualified.

The call c.C::f() uses a qualified name for f, which suppresses dynamic dispatch and the function to which the name resolved is called directly. Since that function is A::f (thanks to the using-declaration), A::f is called non-virtually. The relevant rules follow (quoting C++14 final draft N4140, emphasis mine):

§10.3/15

Explicit qualification with the scope operator (5.1) suppresses the virtual call mechanism.

§5.2.2/1

... If the selected function is non-virtual, or if the id-expression in the class member access expression is a qualified-id, that function is called. Otherwise, its final overrider (10.3) in the dynamic type of the object expression is called; such a call is referred to as a virtual function call.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Moq a base class function from a derived class

From Dev

Use typedef/using from templated base class in derived class

From Dev

Call templated function with derived class arguments using base class pointers

From Dev

How to call a function from a derived class in a base class?

From Dev

returning instance of derived class from base class member function

From Dev

Access to atributes from derived class in the base class

From Dev

Create an instance of derived class from the base class

From Dev

Using Derived Class In a Base Method

From Dev

Accessing virtual base class function from a derived class

From Dev

Calling a member function of a derived class from the base class constructor

From Dev

Create a base class object from a derived class

From Dev

Call derived class' function from a base class' instance

From Dev

Passing derived class to base function

From Dev

Derived class does not call base class method

From Dev

Can a derived class define size of base class array in class declaration?

From Dev

class derived from class template using base:base in body

From Dev

Conflict in return type from base class with derived class using auto

From Dev

Reimplement a virtual function from base class in a derived template class

From Dev

Python: Using derived class attributes in base class

From Dev

Python: Hide member of base class in derived class

From Dev

Call base() from derived class to execute base class function?

From Dev

Base Class Function Not Calling Derived Class Function

From Dev

Creating a derived class from a base

From Dev

Call function in derived class (from interface), from base class

From Dev

Passing derived class to base function

From Dev

Printing from a derived class with a base class function

From Dev

Call function from Derived class stored in a Map with Base class type

From Dev

Call a function from base class that uses the member functions of derived class

From Dev

Calling a derived function from an array of the base class

Related Related

  1. 1

    Moq a base class function from a derived class

  2. 2

    Use typedef/using from templated base class in derived class

  3. 3

    Call templated function with derived class arguments using base class pointers

  4. 4

    How to call a function from a derived class in a base class?

  5. 5

    returning instance of derived class from base class member function

  6. 6

    Access to atributes from derived class in the base class

  7. 7

    Create an instance of derived class from the base class

  8. 8

    Using Derived Class In a Base Method

  9. 9

    Accessing virtual base class function from a derived class

  10. 10

    Calling a member function of a derived class from the base class constructor

  11. 11

    Create a base class object from a derived class

  12. 12

    Call derived class' function from a base class' instance

  13. 13

    Passing derived class to base function

  14. 14

    Derived class does not call base class method

  15. 15

    Can a derived class define size of base class array in class declaration?

  16. 16

    class derived from class template using base:base in body

  17. 17

    Conflict in return type from base class with derived class using auto

  18. 18

    Reimplement a virtual function from base class in a derived template class

  19. 19

    Python: Using derived class attributes in base class

  20. 20

    Python: Hide member of base class in derived class

  21. 21

    Call base() from derived class to execute base class function?

  22. 22

    Base Class Function Not Calling Derived Class Function

  23. 23

    Creating a derived class from a base

  24. 24

    Call function in derived class (from interface), from base class

  25. 25

    Passing derived class to base function

  26. 26

    Printing from a derived class with a base class function

  27. 27

    Call function from Derived class stored in a Map with Base class type

  28. 28

    Call a function from base class that uses the member functions of derived class

  29. 29

    Calling a derived function from an array of the base class

HotTag

Archive