Difference between Superclass::method or this-> method

c++

Solution

using `this->method()` you call a function that is either implemented in your superclass, either by your own class.

When using `superClass::method()`, you make sure to call the one implemented by your parent.

#include <iostream>
#include <string>

class A {
    public:
    void func() {
        std::cout << "A func" << std::endl;
    }
};

class B : A {
    public:                                   
    void func() {
        std::cout << "B func" << std::endl;
    }

    void exec() {
        this->func();
        A::func();
    }
};

int main() {
    B b;

    b.exec();
    return 0;
}

This sample code will output

B func
A func

Problem

How and when would I call a super class method? Please referr to code segment for the two options: ``` class SuperClass { public: void method(); }; class SubClass : public SuperClass { public: void someOtherMethdo(){ this->method(); SuperClass::method(); } }; ```

Original source