C++ 您可以覆盖基类中定义的私有函数吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8103827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can you override private functions defined in a base class?
提问by nitin_cherian
I believe, a derived class
can override
only those functions which it inherited from the base class
. Is my understanding correct.?
我相信,一个derived class
可以override
只它从继承的那些功能base class
。我的理解是否正确。?
That is if base class has a public member function say, func
, then the derived class can override
the member function func
.
也就是说,如果基类有一个公共成员函数,比如说func
,,那么派生类可以override
是成员函数func
。
But if the base class has a private member function say, foo
, then the derived class cannot override the member function foo
.
但是如果基类有一个私有成员函数比如foo
,那么派生类就不能覆盖成员函数foo
。
Am i right?
我对吗?
Edit
编辑
I have come up with a code sample after studying the answers given by SO members. I am mentioning the points which i studied as comments in the code. Hope i am right. Thanks
在研究了 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 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;
}
回答by Xeo
You can override functions regardless of access specifiers. That's also the heart of the non-virtual interfaceidiom. The only requirement is of course that they are virtual
.
无论访问说明符如何,您都可以覆盖函数。这也是非虚拟界面习惯用法的核心。唯一的要求当然是它们是virtual
。
回答by fredoverflow
But if the base class has a private member function say,
foo
, then the derived class cannot override the member functionfoo
.
但是如果基类有一个私有成员函数比如
foo
,那么派生类就不能覆盖成员函数foo
。
In Java, you can't. In C++, you can.
在 Java 中,你不能。在 C++ 中,你可以。
回答by celtschk
You are correct in that to override a function in a derived class, it must inherit it from the base class (in addition, the base class function must be virtual). However you are wrong about your assumption that virtual functions are not inherited. For example, the following works well (and is actually a known idiom for precondition/postcondition checking):
您是正确的,要覆盖派生类中的函数,它必须从基类继承它(此外,基类函数必须是虚拟的)。但是,您对虚函数不是继承的假设是错误的。例如,以下效果很好(并且实际上是用于前置条件/后置条件检查的已知习语):
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;
}
A way to see that private methods are really inherited is to look at the error messages generated by the following code:
查看私有方法是否真正被继承的一种方法是查看以下代码生成的错误消息:
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();
}
Trying to compile this with g++ leads tot he following error messages:
尝试使用 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'
If class Derived wouldn't inherit that function, the error message would be the same in both cases.
如果类 Derived 不继承该函数,则两种情况下的错误消息将相同。
回答by Infintyyy
No You can not over ride any function in base class . My reason for this notion is that if you define the function in derived class that has the same function signiture in base class , the base class function will become hidden for derived class .
否 您不能超越基类中的任何函数。我有这个想法的原因是,如果您在派生类中定义与基类中具有相同函数签名的函数,则基类函数将对派生类隐藏。
For more information about this exciting issue just visit : http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Foverload_member_fn_base_derived.htm
有关这个令人兴奋的问题的更多信息,请访问:http: //publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Foverload_member_fn_base_derived.htm
回答by Andrii
It's also depends on the type of inheritance class Derived : [public | protected | private] Base { };
这也取决于继承的类型 class Derived : [public | protected | private] Base { };
回答by Meysam
In order to override a function inherited from base class, it should be both virtual
, and classified as public
or protected
.
为了覆盖从基类继承的函数,它应该是virtual
, 并归类为public
or protected
。