IaC*_*der 563 c++ oop inheritance
如何使用C++从派生类调用父函数?例如,我有一个名为的类parent,以及一个child从父派生的类.每个班级都有一个print功能.在孩子的打印功能的定义中,我想调用父母的打印功能.我该怎么做呢?
Mot*_*tti 715
我会冒这个明显的风险:你调用函数,如果它在基类中定义,它在派生类中自动可用(除非它是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'
}
};
Run Code Online (Sandbox Code Playgroud)
顺便说一下,你不能两次直接从同一个类派生,因为没有办法引用其中一个基类.
class bottom : public left, public left { // Illegal
};
Run Code Online (Sandbox Code Playgroud)
Gre*_*ill 183
给定父类命名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);
}
Run Code Online (Sandbox Code Playgroud)
请注意,这Parent是类的实际名称而不是关键字.
And*_*ngs 31
如果调用了您的基类Base,并且调用了您的函数,则FooBar()可以直接使用它来调用它Base::FooBar()
void Base::FooBar()
{
printf("in Base\n");
}
void ChildOfBase::FooBar()
{
Base::FooBar();
}
Run Code Online (Sandbox Code Playgroud)
And*_*rey 24
在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)
}
};
Run Code Online (Sandbox Code Playgroud)
使用父作用域解析运算符调用父方法。
父::方法()
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
}
};
Run Code Online (Sandbox Code Playgroud)
如果基类成员函数的访问修饰符是受保护的或公共的,则可以从派生类中调用基类的成员函数。可以从派生成员函数调用基类的非虚拟成员和虚拟成员函数。请参考程序。
#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;
}
Run Code Online (Sandbox Code Playgroud)
输出:
$ 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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
588666 次 |
| 最近记录: |