Virtual functions and polymorphism

后端 未结 6 1117
一向
一向 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:23

    First, A is not an abstract class in your code. It must have at least one pure virtual function to be abstract.

    1. different parameters means completely different method, even though the name is the same. Think of it as a different name. That's why it's called "signature". If A would be an abstract class, this code would not compile at all.

    2. A::hello() will be called. No problem with that, and parameter must be type A, as if there was no inheritance.

提交回复
热议问题