Rockybilly
Rockybilly

Reputation: 4520

How does C++ knows child class calls a parent method?

I don't know how to summarize my question so couldn't find an answer on Google.

When overriding a parent method,

void Child::method(){
    //stuff
    Parent::method();
}

Works perfectly. But the parent method requires the information from the Child class instance to work properly, which is not given as arguments. So my question is, does C++ have a mechanism to let the Parent know which instance of a Child class calls a Parent method, like without doing Parent::method(this) ?

The thing that makes me think this is, if you do something like this,

int main(){
    SomeParentClass::someMethod();
}

It gives: error: call to non-static member function without an object argument. So the compiler needs to know the instance, which was not also given in the child class. But did not raise an error so it must have known the instance.

Edit: I added the Qt tag because I am experimenting on a Qt class. So that I can give an example if needed.

Edit2:

devicedialog.h

class DeviceDialog : public QDialog
{
    Q_OBJECT

public:
    explicit DeviceDialog(QWidget *parent = nullptr);
    int exec() override;
    ~DeviceDialog() override;
}

devicedialog.cpp

int DeviceDialog::exec(){
    // My code.
    return QDialog::exec();
}

exec() function activates and shows a QDialog on the screen. However, that way it is called, it seems like the parent method has no way of knowing which dialog to show (no parameters passed). The only knowledge could be the identity of the instance calling it. I am just asking if this knowledge transferred to the method in the background.

Upvotes: 0

Views: 2259

Answers (3)

Mason
Mason

Reputation: 71

This is still a c++ specific question. Qt just utilizes it. You need to study objects, inheritance, and virtual methods.

void Child::method() {
  Parent::method();
  // this->Parent::method();
}

The above works because the child has access to the parents methods (that are not private). Because the method is an override of the parents virtual method, the only way for the compiler to know you want to call the parents method is to call it with scope resolution '::' in the child.

int main() {
  SomeParentClass::someMethod();
}

The above does not work because there is no instantiated object. When you call a method, you call it on the object (unless it is a static method). So instead you would do something like:

int main() {
  SomeParentClass parent;
  parent.someMethod();

  ChildClass child;
  child.someMethod(); // accesses the childs overridden method and not the parents.
}

Upvotes: 1

Philipp Lenk
Philipp Lenk

Reputation: 961

This is nothing special to member functions of a parent class. Calling a function of the child class via explicitly naming the type:

Child::method();

works in exactly the same way in this regard. Used outside of a member functions definition it causes the same error.

The relevant paragraph of the standard is §9.3.1:

When an id-expression that is not part of a class member access syntax and not used to form a pointer to member is used in a member of class X in a context where this can be used, if name lookup resolves the name in the id-expression to a non-static non-type member of some class C, and if either the id-expression is potentially evaluated or C is X or a base class of X, the id-expression is transformed into a class member access expression using (*this) as the postfix-expression to the left of the . operator.

So, in other words, the call

Parent::method();

inside a member function is transformed into something akin to

(*this).Parent::method();

Upvotes: 2

JaMiT
JaMiT

Reputation: 17073

I think you are just being confused by the language's attempt to make things easier on you. Your example function is equivalent to

void Child::method(){
    //stuff
    this->Parent::method();
}

Just as you cannot call a (non-static) method in the child class without an object, you cannot call the parent's method without an object. However, the context is the body of such a method. Within the body, the context is assumed to be *this. Just as you can refer to data members without explicitly prefixing "this->", you can call methods without the prefix.

Upvotes: 3

Related Questions