Home > Software design >  Implicit object parameter C
Implicit object parameter C

Time:06-03

In this link : Implicit object parameter

In this quote :

If any candidate function is a member function (static or non-static) that does not have an explicit object parameter (since C 23), but not a constructor, it is treated as if it has an extra parameter (implicit object parameter) which represents the object for which they are called and appears before the first of the actual parameters.

I do not understand Why does the word static is mentioned here ? isn't Implicit object parameter the ( this ) pointer ( which ( the ( this ) pointer ) only works with non-static functions ) ?

Edit in this link : link

quote :

The keyword this is a rvalue (until C 11)prvalue (since C 11) expression whose value is the address of the implicit object parameter (object on which the non-static member function is being called). It can appear in the following contexts:

CodePudding user response:

Consider what happens if you don't have this rule and have a static method and non-static method with the same (explicit) parameters. Then to the non-static method an additional implicit parameter (this) will be added, but not to the static method. This will make the list of parameters of both methods different and will allow to overload the static method with non-static method with the same explicit parameters.

CodePudding user response:

First things first, there is a difference between implicit object parameter and implicit this parameter. The former is a reference type while the latter is a pointer type. The standard mostly1 talks about "implicit object parameter" during overload resolution and not "implicit this parameter". You can confirm that this is the case here.

In other words, implicit object parameter is not the same as this. For more reference on this you can refer to Contradicting definition of implicit this parameter in the standard


isn't Implicit object parameter the ( this ) pointer ( which ( the ( this ) pointer ) only works with non-static functions )

No, both static as well as non static member functions have an implicit object parameter for the purposes of overload resolution as can be seen from over.match.funcs#2 which states:

The set of candidate functions can contain both member and non-member functions to be resolved against the same argument list. So that argument and parameter lists are comparable within this heterogeneous set, a member function is considered to have an extra parameter, called the implicit object parameter, which represents the object for which the member function has been called. For the purposes of overload resolution, both static and non-static member functions have an implicit object parameter, but constructors do not.

(emphasis mine)

Sidenote

1Even though the standard actually does mention the phrase "implicit this parameter" as can be seen here it is very likely an editorial mistake/issue which i've reported to std-proposal where it was confirmed that is indeed defective wording. My take from this is that it will be fixed in the future versions.

  •  Tags:  
  • c
  • Related