如何使用 C++ 从派生类调用父函数?例如,我有一个名为 parent
的类,以及一个从父级派生的名为 child
的类。每个类中都有一个 print
函数。在孩子的打印功能的定义中,我想调用父母的打印功能。我该怎么做呢?
我将冒险说明这一点:您调用该函数,如果它在基类中定义,则它在派生类中自动可用(除非它是 private
)。
如果派生类中存在具有相同签名的函数,您可以通过添加基类名称后跟两个冒号 base_class::foo(...)
来消除歧义。您应该注意,与 Java 和 C# 不同,C++ 没有 为“基类”(super
或 base
)提供关键字,因为 C++ 支持可能导致歧义的 multiple inheritance。
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
是类的实际名称,而不是关键字。
foo()
是类似于 print()
还是一个单独的函数?您的意思是使用 private
继承来隐藏从基础继承的细节,并为您确实想要公开的事物提供 public
阴影功能吗?
foo()
类似于 print()
。让我回到使用 print()
,因为我认为在这种情况下它会更有意义。假设某人创建了一个类,该类对特定数据类型执行一组操作,公开了一些访问器,并有一个 print(obj&)
方法。我需要一个在 array-of-obj
上工作的新课程,但其他一切都是一样的。组合会导致大量重复的代码。继承将在 print(array-of-obj&)
循环中调用 print(obj&)
的情况最小化,但不希望客户端调用 print(obj&)
,因为他们这样做没有意义
如果您的基类称为 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)
}
};
typdef
像 super
这样的父级。
__super
的使用是合理的;我在这里提到它作为替代建议。开发人员应该了解他们的编译器并了解其功能的优缺点。
使用父范围解析运算符调用父方法。
父::方法()
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 OR 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
virtual
的一些示例!
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();
}
参考例子。
不定期副业成功案例分享
template<class A, class B> class C: public A, public B {};
可能有两种类型相同,原因取决于您的代码的使用方式(这使得 A 和 B 相同),可能是两个或三个抽象层的方式来自不知道你做了什么的人。using namespace std
。You should note that unlike Java and C#, C++ does not have a keyword for "the base class"
。