C ++を使用して派生クラスから親関数を呼び出す方法たとえば、私はと呼ばれるクラスがありますparentと呼ばれるクラスchildこれは親から派生しています。内に
各クラスがありますprint関数。子供の印刷機能の定義で、私は両親の印刷機能を呼び出したいと思います。これをどのようにして行うのでしょうか。
私は明白なことを述べる危険性を冒します:あなたがその関数を基本クラスで定義されているなら、あなたはそれが派生クラスで自動的に利用可能であると呼びますprivate)
派生クラスに同じシグネチャを持つ関数がある場合は、基本クラスの名前とそれに続く2つのコロンを追加することによってそれを明確にすることができます。base_class::foo(...)。 JavaやC#とは異なり、C ++では機能します。ではない「基本クラス」のキーワードがある(superまたはbaseC + +をサポートしているため)多重継承あいまいさにつながる可能性があります。
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'
}
};
ちなみに、同じクラスから直接2回派生させることはできません。基本クラスの1つを他のクラスから参照する方法がないからです。
class bottom : public left, public left { // Illegal
};
template<class A, class B> class C: public A, public B {};コードの使い方(AとBを同じにする)に応じて、2つのタイプが同じ理由で同じになることがあります。 - Emilio Garavagliausing namespace std。 - JAB
指定された親クラスParentという名前の子クラスChildあなたはこのようなことをすることができます:
class Parent {
public:
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クラスの実際の名前であり、キーワードではありません。
parent親クラスの実際の名前です。親クラスが呼び出された場合widgetそれならあなたは電話するでしょうwidget::print(x);。 - Greg Hewgillfoo()ここに類似したprint()それとも別の機能?そして使うということですかprivateベースから継承された詳細を隠すための継承publicあなたのためのシャドウイング関数行う公開したいですか? - underscore_d
基本クラスが呼び出された場合Baseそしてあなたの関数は呼ばれますFooBar()あなたはそれを使用して直接それを呼び出すことができますBase::FooBar()
void Base::FooBar()
{
printf("in Base\n");
}
void ChildOfBase::FooBar()
{
Base::FooBar();
}
MSVCには、そのためのMicrosoft固有のキーワードがあります。__スーパー
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。 - Thomas Eding__super;私はそれを別の提案としてここで述べました。開発者は自分のコンパイラを知っていて、その機能の長所と短所を理解する必要があります。 - Andrey
基本クラスメンバ関数のアクセス修飾子が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();
}
参考例です。