你可以覆盖基类中定义的私有函数吗?

时间:2011-11-12 10:18:09

标签: c++ override derived-class base-class

我相信,derived class override只能base class继承自func的那些功能。我的理解是否正确。?

即如果基类具有公共成员函数,例如override,那么派生类可以func成员函数foo

但是如果基类有一个私有成员函数,例如foo,那么派生类不能覆盖成员函数/* Points to ponder: 1. Irrespective of the access specifier, the member functions can be override in base class. But we cannot directly access the overriden function. It has to be invoked using a public member function of base class. 2. A base class pointer holding the derived class obj's address can access only those members which the derived class inherited from the base class. */ #include <iostream> using namespace std; class base { private: virtual void do_op() { cout << "This is do_op() in base which is pvt\n"; } public: void op() { do_op(); } }; class derived: public base { public: void do_op() { cout << "This is do_op() in derived class\n"; } }; int main() { base *bptr; derived d; bptr = &d; bptr->op(); /* Invoking the overriden do_op() of derived class through the public function op() of base class */ //bptr->do_op(); /* Error. bptr trying to access a member function which derived class did not inherit from base class */ return 0; }

我是对的吗?

修改

在研究了SO成员给出的答案后,我想出了一个代码示例。我提到我在代码中作为评论研究的要点。希望我是对的。感谢

{{1}}

6 个答案:

答案 0 :(得分:10)

无论访问说明符如何,您都可以覆盖函数。这也是non-virtual interface成语的核心。唯一的要求当然是virtual

答案 1 :(得分:10)

  

但是如果基类有一个私有成员函数,例如foo,那么派生类不能覆盖成员函数foo

在Java中,你不能。在C ++中,你可以。

答案 2 :(得分:3)

你是正确的,要覆盖派生类中的函数,它必须从基类继承它(此外,基类函数必须是虚拟的)。但是,您假设虚函数不是继承的,那就错了。例如,以下方法效果很好(实际上是前置条件/​​后置条件检查的已知习惯用法):

class Base
{
public:
  void operate_on(some thing);
private:
  virtual void do_operate_on(some thing) = 0;
};

void Base::operate_on(some thing)
{
  // check preconditions
  do_operate_on(thing);
  // check postconditions
}

class Derived: public Base
{
  // this overrides Base::do_operate_on
  void do_operate_on(some thing);
};

void Derived::do_operate_on(some thing)
{
  // do something
}

int main()
{
  some thing;
  Base* p = new Derived;

  // this calls Base::operate_on, which in turn calls the overridden
  // Derived::do_operate_on, not Base::do_operate_on (which doesn't have an
  // implementation anyway)
  p->operate_on(thing);

  delete p;
}

查看私有方法是否真正被继承的方法是查看由以下代码生成的错误消息:

class Base
{
private:
  void private_method_of_B();
};

class Derived:
  public Base
{
};

int main()
{
  Derived d;
  d.private_method_of_B();
  d.method_that_does_not_exist();
}

尝试用g ++编译它会导致他跟随错误消息:

privatemethodinheritance.cc: In function 'int main()':
privatemethodinheritance.cc:4: error: 'void Base::private_method_of_B()' is private
privatemethodinheritance.cc:15: error: within this context
privatemethodinheritance.cc:16: error: 'class Derived' has no member named 'method_that_does_not_exist'

如果Derived类不会继承该函数,则两种情况下的错误消息都是相同的。

答案 3 :(得分:0)

否你不能超越基类中的任何功能。 我的理由是,如果在派生类中定义具有基类中相同函数签名的函数,则基类函数将对派生类隐藏。

  

有关这个令人兴奋的问题的更多信息,请访问:   http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Foverload_member_fn_base_derived.htm

答案 4 :(得分:-2)

这还取决于继承的类型
class Derived : [public | protected | private] Base { };

答案 5 :(得分:-3)

为了覆盖从基类继承的函数,它应该都是virtual,并归类为publicprotected