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

后端 未结 6 1390
庸人自扰
庸人自扰 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:38

    Correction to Dennis's post.

    Note you state as a solution you can cast the animal (which is a Animal*) to a Cow*. However, once a Cow* your printMessage from the Animal class will not be available. Therefore ((Cow*)animal)->printMessage() needs to be ((Cow*)animal)->mispelledPrintMessage()

提交回复
热议问题