Virtual functions and polymorphism

后端 未结 6 1115
一向
一向 2021-01-24 00:33

Suppose I have this:

class A
{
    public:
    virtual int hello(A a);
};

class B : public A
{
   public:
   int hello(B b){ bla bla };
};

So,

6条回答
  •  一整个雨季
    2021-01-24 01:12

    When you override a method, it redefines what the method will do. You can only override virtual members that are already defined (with their set of parameters). If the type is of A, the method on A will be called. If the type is of B, the method on B will be called even if the variable is typed A but contains an instance of type B.

    You can't change the parameter definitions for an overridden method, or else it would cease to be an override.

提交回复
热议问题