0

What am I missing? Why won't the compiler realize that I'm calling the base class version of getTemperature() with no parameters? Tried on godbolt, gcc and clang don't find it. Hmmph. (It's that getTemperature() call inside sayTemperature(...) btw)

#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());}
};
asmmo
  • 6,922
  • 1
  • 11
  • 25
Steger
  • 887
  • 7
  • 16
  • 1
    https://stackoverflow.com/questions/1628768/why-does-an-overridden-function-in-the-derived-class-hide-other-overloads-of-the – Mat Apr 06 '20 at 20:46
  • You nailed it. Quirk of the language. Should have known that someone else has run into this one before. – Steger Apr 06 '20 at 20:47

1 Answers1

1

The function call here will be directed to the function in the scope of the class you are calling from. So, use the base class and scope operator to do what you need

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

    std::string sayTemperature() {return "Patient's temperature is " + std::to_string(Person::getTemperature());}
};
asmmo
  • 6,922
  • 1
  • 11
  • 25