What are the rules for virtual function lookup?

前端 未结 3 1872
忘掉有多难
忘掉有多难 2021-01-19 02:37
#include 
class base
{
    public:
    virtual void print (int a)
    {   
        std::cout << \"a: \" << a << \" base\\n\";
    }         


        
3条回答
  •  醉话见心
    2021-01-19 02:53

    Overload resolution happens at compile time. Overrides happen at run time.

    Therefore, the overload resolution of b->print(d); happens first. This selects Base::print(int) because it's the only one-argument print.

    At runtime, b points to a Derived object that has no override for Base::print(int). Therefore, Base::print(int) is still called.

提交回复
热议问题