virtual function that is const in the base class and not const in the derived

后端 未结 6 1374
庸人自扰
庸人自扰 2021-02-08 09:54

Can anyone explain the output of the following code?

#include 
#include 
class Animal
{
public:
    Animal(const std::string &          


        
6条回答
  •  北海茫月
    2021-02-08 10:45

    You have two different versions of printMessage, one which is const and one which isn't. The two are unrelated, even though they have the same name. The new function in Cow hides the one in Animal, so when you call it directly the compiler only considers the Cow version.

提交回复
热议问题