How to call a parent class function from derived class function?

后端 未结 7 1097
自闭症患者
自闭症患者 2020-11-22 06:16

How do I call the parent function from a derived class using C++? For example, I have a class called parent, and a class called child which is deri

相关标签:
7条回答
  • 2020-11-22 06:56
    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();
    }
    

    Reference example.

    0 讨论(0)
  • 2020-11-22 06:57

    In MSVC there is a Microsoft specific keyword for that: __super


    MSDN: Allows you to explicitly state that you are calling a base-class implementation for a function that you are overriding.

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

    0 讨论(0)
  • 2020-11-22 06:58

    Call the parent method with the parent scope resolution operator.

    Parent::method()

    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
        }
    };
    
    0 讨论(0)
  • 2020-11-22 07:08

    I'll take the risk of stating the obvious: You call the function, if it's defined in the base class it's automatically available in the derived class (unless it's private).

    If there is a function with the same signature in the derived class you can disambiguate it by adding the base class's name followed by two colons base_class::foo(...). You should note that unlike Java and C#, C++ does not have a keyword for "the base class" (super or base) since C++ supports multiple inheritance which may lead to ambiguity.

    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'
        }
    };
    

    Incidentally, you can't derive directly from the same class twice since there will be no way to refer to one of the base classes over the other.

    class bottom : public left, public left { // Illegal
    };
    
    0 讨论(0)
  • 2020-11-22 07:12

    If your base class is called Base, and your function is called FooBar() you can call it directly using Base::FooBar()

    void Base::FooBar()
    {
       printf("in Base\n");
    }
    
    void ChildOfBase::FooBar()
    {
      Base::FooBar();
    }
    
    0 讨论(0)
  • 2020-11-22 07:13

    Given a parent class named Parent and a child class named Child, you can do something like this:

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

    Note that Parent is the class's actual name and not a keyword.

    0 讨论(0)
提交回复
热议问题