22

我可以理解,可能有理由将已实现的(而不是纯的)虚函数声明为私有或受保护的。Afaik,如果您将实现的虚拟方法声明为受保护,您的子类可以调用基类的方法(而其他人不能)。如果将其声明为私有,则只有基类可以调用虚拟方法的默认实现。

但是,对于纯虚拟,没有基本实现......那么将纯虚拟声明为私有或受保护在功能上不等效吗?受保护的纯虚拟没有意义,因为您永远无法调用基类的相应方法。受保护的纯虚拟是否有任何意义?

SO上有一些类似的主题,但我找不到任何可以简明扼要地回答我的问题的东西。

4

2 回答 2

11

受保护的纯虚拟是否有任何意义?

我认为你的意思是私人这里(而不是受保护),但我想我理解你的意思。事实上,纯虚拟的访问类型可以在派生类中被覆盖。这是一个示例,可以帮助您了解私有和受保护的纯虚拟之间的区别:

class Parent
{
  protected: virtual void foo() = 0;
  private:   virtual void bar() = 0;
  public:            void test() { foo(); bar(); }
};

class Child : public Parent
{
  public: void test2() { foo(); /* bar(); // cannot be called here */ }
};

class GrandChild : public Child
{
  // access types here can be anything for this example
  public: void foo() { cout << "foo" << endl; }
  public: void bar() { cout << "bar" << endl; }
};
于 2012-03-19T02:31:32.647 回答
4

第一个纯虚函数可以实现!

#include <iostream>

class Animal
{
public:
  void eat(void);
protected:
  virtual void doEat(void)=0;
};
void Animal::eat(void)
{
  doEat();
}
void Animal::doEat(void)
{
  std::cout << "animal" << std::endl;
}

class Tiger : public Animal
{
private:
  virtual void doEat(void)
  {
    Animal::doEat();//here is the difference between protected and private
    std::cout << "tiger" << std::endl;
  }
};

int main(void)
{
  Animal *p = new Tiger();
  p->eat();
  return 0;
}

其次,Herb Sutter 解释了何时使用“virtual private”或“virtual protected”,你可以从这篇文章中阅读。我认为这解释了为什么我们不仅可以这样做!文章说:“更喜欢将虚拟函数设为私有,只有派生类需要调用虚函数的基实现,才能使虚函数受保护”,你的问题是关于纯虚函数的,我不太确定是否满足这个原则。

于 2013-01-23T09:02:11.057 回答