c++ - 派生类不会覆盖具有不同签名的虚函数

标签 c++ virtual-functions overriding function-signature

我有一个派生类,我希望其中一个函数覆盖其在基类中的版本,但具有不同的签名。 简单示例:

#include "stdio.h"

bool use_foo = false;

class Foo {
public:
    virtual int func(double x) { printf ("%f in Foo!\n", x); }
};

class Bar : public Foo {
public:
    int func(short x) { printf ("%d in Bar!\n", x); }
};

int main () {
    Foo* A;
    if (use_foo) 
        A = new Foo;
    else 
        A = new Bar;

    A->func(2);
    return 0;
}

即使 A 被分配为派生类,上面的代码也会调用基类拷贝:

> g++ test.cpp -o test -O3 && ./test
2.000000 in Foo!

因为(就我的理解而言)可以转换参数以匹配基类签名,并且派生类不会因为这种差异而覆盖它(但它不会隐藏 在那种情况下?)。如果我将基类函数更改为也将 short 作为参数,派生类会设法覆盖它。

有没有一种简单的方法可以说服调用基于指针使用正确的函数?我可以像这样添加另一个功能:

class Bar : public Foo {
public:
    int func2(short x) { printf ("%d in Bar!\n", x); }
    int func(double x) { func2(x); }
};

但是我会一直转换参数(short->double->short),这个函数对性能很关键。有没有更好的办法?

最佳答案

这些函数签名不相同:

virtual int func(double x) {...} // base class
int func(short x) {...} // derived class

一个使用double参数,另一个使用short。对于 overriding要发生必须满足几个条件。基函数和派生函数的相同参数类型就是其中之一。 Bellow 是 Scott Meyers 关于所有要求的“Modern Effective C++”一书的摘录:

• The base class function must be virtual.

• The base and derived function names must be identical (except in the case of destructors).

• The parameter types of the base and derived functions must be identical.

• The constness of the base and derived functions must be identical.

• The return types and exception specifications of the base and derived functions must be compatible.

或者,使签名相同并在派生函数体内执行转换:

int func(double x) override {
    short temp = static_cast<short>(x);
    // ...
}

关于c++ - 派生类不会覆盖具有不同签名的虚函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49465254/

相关文章:

c++ - LNK2005 : delete already defined error in VC++

c++ - 如何避免在此模板代码中出现有关被零除的警告?

javascript - 覆盖javascript中的全局函数

c++ - 部分隐藏的继承树

c++ - 使用模板的函数重载 - 仅在存在替代函数时应用 enable_if

c++ - 覆盖 C++ 虚方法

c++ - 从文件中读取对象后无法调用虚拟成员函数

c++ - 为什么具有虚函数的类与没有虚函数的类对齐方式不同?

java - 自定义 equals() 方法无法正常工作

c# - 正确渲染覆盖的 TreeView