你能重写基类中定义的私有函数吗
Can you override private functions defined in a base class?
我相信,derived class
只能override
继承自base class
的那些函数。我的理解正确吗。?
也就是说,如果基类有一个公共成员函数,比如func
,那么派生类可以override
成员函数func
。
但是,如果基类有一个私有成员函数,比如foo
,那么派生类就不能覆盖成员函数foo
。
我说得对吗?
编辑
在研究了SO成员给出的答案后,我想出了一个代码示例。我提到的是我在代码中作为注释研究的要点。希望我是对的。感谢
/* 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 pvtn";
}
public:
void op()
{
do_op();
}
};
class derived: public base
{
public:
void do_op()
{
cout << "This is do_op() in derived classn";
}
};
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;
}
无论访问说明符如何,都可以重写函数。这也是非虚拟界面习惯用法的核心。唯一的要求当然是它们是virtual
。
但是,如果基类有一个私有成员函数,比如
foo
,那么派生类就不能覆盖成员函数foo
。
在Java中,你不能。在C++中,你可以。
您是正确的,要覆盖派生类中的函数,它必须从基类继承它(此外,基类函数必须是虚拟的(。然而,您错误地认为虚拟函数不是继承的。例如,以下操作很好(实际上是前置条件/后置条件检查的一个已知习惯用法(:
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类不会继承该函数,那么在这两种情况下,错误消息都是相同的。
否不能超越基类中的任何函数。我提出这个概念的原因是,如果你在派生类中定义了在基类中具有相同函数签名的函数,那么基类函数将对派生类隐藏。
有关这个令人兴奋的问题的更多信息,请访问:http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Foverload_member_fn_base_derived.htm
这也取决于继承的类型class Derived : [public | protected | private] Base { };
为了覆盖从基类继承的函数,它应该既是virtual
,又分类为public
或protected
。
- 不同翻译单元中不可重载的非内联函数定义
- Visual Studio中的函数声明和函数定义问题
- 编写代码时C++出现错误:错误 1 错误 C2601:'circle':本地函数定义是非法的
- 具有enable_if外部类原型的模板类构造函数定义
- 类的前向声明之后的类成员函数定义,在类声明之前
- 为函数定义符号不明确的指针参数
- C++模板专用化 - 无法匹配函数定义
- 错误:在第 6 行'{'标记之前,此处不允许使用函数定义
- 找不到 #define 的函数定义
- 根据类型特征更改函数定义?
- 将抽象基类中的所有纯虚函数定义为 varaidaic 模板
- 命名空间更改函数定义
- "Type&"与C++函数定义中的"Type*"
- C++:为什么允许在另一个函数中声明函数,而不允许在函数定义中声明?
- 如何从 C++ 中的现有模板函数定义新函数
- 私有在函数定义/实现的返回值范围内是什么意思 (c++)?
- 越界成员函数定义是否需要一个完全限定的类名,直到全局范围
- 为什么c++允许成员函数定义中实例的私有成员访问
- Qt基类函数定义
- C++函数定义中参数列表后面额外一对括号的含义