Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Superclass::method or this-> method

Tags:

c++

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();
 }
};
like image 725
Billybonks Avatar asked Oct 24 '12 13:10

Billybonks


4 Answers

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
like image 73
tomahh Avatar answered Oct 14 '22 01:10

tomahh


this->method();

...calls method on the derived class (and is the same as writing simply method();). This could call the inherited method from the parent (and does in the example), or it could call an overridden version in the child class (and will, if one exists).

SuperClass::method();

...will always call the method on the parent. This syntax is often used within an override in the child, when you want to keep and extend the functionality of the parent class method. E.g.

Class SubClass: public SuperClass {
  //...
  void method() {
    SuperClass::method();
    //...
  }
};

Note that if you use the first syntax in this second case, you'll get recursion.

Note also that this has nothing to do with virtual methods. Marking a method as virtual means that, when the method is called via a base class pointer, the most-derived class method available will be called. In the code above, it makes no difference whether any of the methods are virtual since there are no base class pointers involved.

like image 42
sje397 Avatar answered Oct 14 '22 02:10

sje397


this->method

Will default to the local implementation in your derived class first, if that one isn't present, it will take the superclass method. If that one is not present it will give a compilation error.

superClass::method()

Will always direct to the method in your superclass

In most cases you want this->method. superClass::method() Is usefull when part of a method is implemented in the superclass and you want to extend it in the derived class. something like:

Class SubClass : public SuperClass {
public:
 void someOtherMethdo(){
   SuperClass::someOtherMethdo();
   //Rest of the method
 }
}
like image 32
Minion91 Avatar answered Oct 14 '22 01:10

Minion91


this->method() leaves room for ambiguity under certain circumstances (e.g. if more than one ancestor defines method with this signature), but at the same time allows method to be called no matter where exactly it is defined. If method is virtual it will call the most-derived version.

SuperClass::method() is explicit. It will either call that specific method or give a compiler error.

like image 43
Jon Avatar answered Oct 14 '22 01:10

Jon