首页 文章

如何从派生类函数调用父类函数?

提问于
浏览
485

如何使用C从派生类调用父函数?例如,我有一个名为 parent 的类,以及一个名为 child 的类,它是从父类派生的 . 在每个类中都有一个 print 函数 . 在孩子的打印功能的定义中,我想调用父母的打印功能 . 我该怎么做呢?

6 回答

  • 19

    如果您的基类名为 Base ,并且您的函数名为 FooBar() ,则可以使用 Base::FooBar() 直接调用它

    void Base::FooBar()
    {
       printf("in Base\n");
    }
    
    void ChildOfBase::FooBar()
    {
      Base::FooBar();
    }
    
  • -12

    在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)
       }
    };
    

  • 3

    我'll take the risk of stating the obvious: You call the function, if it'在基类中定义它's automatically available in the derived class (unless it' s private ) .

    如果派生类中存在具有相同签名的函数,则可以通过添加基类的名称后跟两个冒号 base_class::foo(...) 来消除歧义 . 您应该注意,与Java和C#不同,C确实 not 具有"the base class"( superbase )的关键字,因为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
    };
    
  • 624

    给定名为 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 是类的实际名称,而不是关键字 .

  • 27

    如果基类成员函数的访问修饰符受保护或公共,则可以从派生类调用基类的成员函数 . 可以从派生成员函数调用基类非虚拟和虚拟成员函数 . 请参考该计划 .

    #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
    
  • 163
    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();
    }
    

    参考例子 .

相关问题