How to access protected method in base class from derived class?

后端 未结 6 507
孤街浪徒
孤街浪徒 2020-12-17 22:18

Here is a sample of code that annoys me:

class Base {
  protected:
    virtual void foo() = 0;
};

class Derived : public Base {
  private:
    Base *b; /* I         


        
6条回答
  •  旧巷少年郎
    2020-12-17 22:56

    You call base functions explicitly with the scope operator (Base::foo()). But in this case, the Base class doesn't define foo (it's pure virtual), so there's actually no function to execute when you say this->b->foo(); since b is a pointer to Base and not Derived.

提交回复
热议问题