![](/img/trans.png)
[英]Call derived class function from parent class without knowing derived class
[英]How to call a parent class function from derived class function?
如何使用 C++ 从派生类调用父函数? 例如,我有一个名为parent
的类,以及一个从 parent 派生的名为child
的类。 在每个类中都有一个print
功能。 在孩子的打印功能的定义中,我想调用父母的打印功能。 我该怎么做呢?
我将冒着说明显而易见的风险:您调用该函数,如果它是在基类中定义的,则它在派生类中自动可用(除非它是private
)。
如果派生类中有一个具有相同签名的函数,您可以通过添加基类名称后跟两个冒号base_class::foo(...)
来消除歧义。 你应该注意到,不像Java和C#,C ++不具备“基础类”(关键字super
或base
,因为C ++的支持),多重继承,这可能导致歧义。
class left {
public:
void foo();
};
class right {
public:
void foo();
};
class bottom : public left, public right {
public:
void foo()
{
//base::foo();// ambiguous
left::foo();
right::foo();
// and when foo() is not called for 'this':
bottom b;
b.left::foo(); // calls b.foo() from 'left'
b.right::foo(); // call b.foo() from 'right'
}
};
顺便说一下,你不能直接从同一个类派生两次,因为没有办法引用一个基类而不是另一个基类。
class bottom : public left, public left { // Illegal
};
给定一个名为Parent
的父类和一个名为Child
的子类,您可以执行以下操作:
class Parent {
public:
virtual void print(int x);
};
class Child : public Parent {
void print(int x) override;
};
void Parent::print(int x) {
// some default behavior
}
void Child::print(int x) {
// use Parent's print method; implicitly passes 'this' to Parent::print
Parent::print(x);
}
请注意, Parent
是类的实际名称,而不是关键字。
如果您的基类称为Base
,而您的函数称为FooBar()
您可以使用Base::FooBar()
直接调用它
void Base::FooBar()
{
printf("in Base\n");
}
void ChildOfBase::FooBar()
{
Base::FooBar();
}
在 MSVC 中有一个 Microsoft 特定的关键字: __super
MSDN:允许您明确声明您正在为要覆盖的函数调用基类实现。
// deriv_super.cpp
// compile with: /c
struct B1 {
void mf(int) {}
};
struct B2 {
void mf(short) {}
void mf(char) {}
};
struct D : B1, B2 {
void mf(short) {
__super::mf(1); // Calls B1::mf(int)
__super::mf('s'); // Calls B2::mf(char)
}
};
使用父作用域解析运算符调用父方法。
父::方法()
class Primate {
public:
void whatAmI(){
cout << "I am of Primate order";
}
};
class Human : public Primate{
public:
void whatAmI(){
cout << "I am of Human species";
}
void whatIsMyOrder(){
Primate::whatAmI(); // <-- SCOPE RESOLUTION OPERATOR
}
};
如果基类成员函数的访问修饰符是protected 或public,则可以从派生类调用基类的成员函数。 可以从派生成员函数调用基类非虚和虚成员函数。 请参考程序。
#include<iostream>
using namespace std;
class Parent
{
protected:
virtual void fun(int i)
{
cout<<"Parent::fun functionality write here"<<endl;
}
void fun1(int i)
{
cout<<"Parent::fun1 functionality write here"<<endl;
}
void fun2()
{
cout<<"Parent::fun3 functionality write here"<<endl;
}
};
class Child:public Parent
{
public:
virtual void fun(int i)
{
cout<<"Child::fun partial functionality write here"<<endl;
Parent::fun(++i);
Parent::fun2();
}
void fun1(int i)
{
cout<<"Child::fun1 partial functionality write here"<<endl;
Parent::fun1(++i);
}
};
int main()
{
Child d1;
d1.fun(1);
d1.fun1(2);
return 0;
}
输出:
$ g++ base_function_call_from_derived.cpp
$ ./a.out
Child::fun partial functionality write here
Parent::fun functionality write here
Parent::fun3 functionality write here
Child::fun1 partial functionality write here
Parent::fun1 functionality write here
struct a{
int x;
struct son{
a* _parent;
void test(){
_parent->x=1; //success
}
}_son;
}_a;
int main(){
_a._son._parent=&_a;
_a._son.test();
}
参考示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.