简体   繁体   English

重载虚拟函数并通过指向基类的指针调用派生函数

[英]overloading virtual function and calling derived function by pointer to base class

#include <bits/stdc++.h>
using namespace std;

class Base {
  public:
  virtual int function() const {
    cout << "Base::function()\n";
    return 1;
  }
  virtual void function(string) const {}
};


class Derived : public Base {
  public:
       // overloading function() of base
       int function(int) const {
         cout << "Derived::function()\n";
         return 4;
    }
};


int main()
{
    string s("StackOverflow");
    Derived d;
    Base* b = &d;
    //calling derived::function() and function(s)    
    b->function();
    b->function(s);
}

Because of overloading, name hiding occour in derived class. 由于重载, 名称隐藏在派生类中。
Because of keyword virtual at runtime Derived::function() should be called. 由于在运行时使用关键字virtual ,因此应调用Derived :: function()。

But the code compiles successfully. 但是代码编译成功。 link : http://ideone.com/fbVm0P 链接: http//ideone.com/fbVm0P
What is the reason for this strange behaviour? 这种奇怪行为的原因是什么?
EDIT 1: 编辑1:

d.function();  
d.function(s);  

do not compile,as expected. 不按预期编译。

Overload resolution uses the static types (not that it would make a difference here). 重载解析使用静态类型(不是在这里会有所作为)。 So both b->function() and b->function( s ) resolve to Base::function , with no error. 因此, b->function()b->function( s )解析为Base::function ,没有错误。 Finally, since these functions have been declared virtual , the final resolution will take into account any overloads in a derived class. 最后,由于已将这些函数声明为virtual ,因此最终解决方案将考虑派生类中的所有重载。 But there aren't any, so the function in the base class will be called. 但是没有,因此将调用基类中的函数。

Name hiding occurs during name lookup, which is before overload resolution, and also only concerns the static type; 名称隐藏发生在名称查找过程中,该过程在重载解析之前,并且仅涉及静态类型。 in an expression like b->function() or b->function( s ) , the compiler ignores Derived completely; 在类似b->function()b->function( s )的表达式中,编译器完全忽略Derived it does the name lookup on the static type. 它在静态类型上进行名称查找。 Name hiding would only come into consideration is the static type were Derived ; 仅当静态类型Derived时才考虑名称隐藏; once the compiler found function in Derived , it would look no further. 一旦编译器在Derived找到function ,就再也找不到了。

The global rules are fairly simple: name lookup (using static type), then overload resolution (using static type), and finally, if the resolved name is a virtual function, dynamic determination of the actual function depending on the dynamic type. 全局规则非常简单:名称查找(使用静态类型),然后重载解析(使用静态类型),最后,如果解析的名称是虚拟函数,则根据动态类型动态确定实际函数。

There is no int function() const in the derived class. 派生类中没有int function() const

Use the override keyword to avoid surprises like this, ie, 使用override关键字可避免此类意外情况,即

class Derived
    : public Base
{
public:
    // not overriding function() of base
    auto function(int) const
        -> int override // This won't compile, because it doesn't override.
    {
         cout << "Derived::function()\n";
         return 4;
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM