无法调用具有不同签名的基类方法

时间:2020-04-06 20:43:16

标签: c++ oop

我想念什么?为什么编译器不会意识到我正在调用不带参数的getTemperature()的基类版本?尝试过Godbolt,gcc和clang找不到它。 m (就是在sayTemperature(...)btw中调用getTemperature()

#include <string>

struct Person
{
    virtual double getTemperature(int day) {return (day & 1) ? 98.5 : 98.6;}

    double getTemperature() {return 98.7;}
};

struct Patient : public Person
{
    double getTemperature(int day) {return 98.8; }

    std::string sayTemperature() {return "Patient's temperature is " + std::to_string(getTemperature());}
};

1 个答案:

答案 0 :(得分:1)

此处的函数调用将定向到您要从中调用的类范围内的函数。因此,请使用基类和范围运算符执行所需的操作

struct Patient : public Person
{
    double getTemperature(int day) {return 98.8; }

    std::string sayTemperature() {return "Patient's temperature is " + std::to_string(Person::getTemperature());}
};
相关问题