The method call is explicit and implicit . How do they differ? If I understand correctly, explicit it is something like:

obj.meth(x, y); 

What then is not an obvious challenge? And what methods can not be called explicitly?

  • Perhaps this is a function call on a pointer to it? (Not sure, because I don't know.) - VladD
  • Call on the pointer? There is a call by reference and call by value. Call on the pointer - I do not know. - Andrew Kachalin
  • In scala, for example, an implicit conversion method can be called if at least one argument does not fit the signature of the method. But not sure what you mean by that. - etki
  • @Etki, yes, this example is great. - Igor Karpenko
  • one
    @AndrewKachalin: Have you ever programmed in C? - VladD

1 answer 1

Here's an example: in PHP, there is a __toString magic method, implementing this method in a class, we specify how an instance of the class should present itself as a string. When an object of a class with a specific __toString method is called in a context that assumes that the work goes with a string, the magic method __toString is implicitly called before performing other actions on the object.

 class SomeClass { public function __toString() { return 'Hello World!'; } } $a = new SomeClass(); echo $a; // outputs 'Hello World!' 

In line with echo $a; at first, the SomeClass::__toString() method will be called implicitly, although we did not explicitly give such instructions, and then echo will be executed.

There are many more examples from different languages, but I think the basic idea should be clear.

UPD

Explicit method call - sorry for taftology, you clearly indicate in your code which method and with what arguments you want to call. An implicit call — the compiler (or interpreter) does it implicitly for you. In C ++, I haven’t written anything for a long time, so unfortunately I can’t give an example of an implicit method call, but I remembered, probably the most well-known and easy-to-understand example of the “implicit” in C ++ is this , a pointer to an instance of a class, all member functions of a class in C ++ (except static) implicitly takes as an argument a pointer to an instance of the class in the context of which the method is accessed.

  • one
    Can you give examples from java or C? But it is not very clear so far. - Andrew Kachalin
  • And do you know examples of methods (such as there are in some languages) that cannot be called explicitly? Also a very important component. And anyway, thanks, plus, it is already starting to come up. - Andrew Kachalin
  • I haven’t written in X for a long time, now I’ll try to remember or find examples and update the answer. - Igor Karpenko
  • On C - not necessary. He is not an OOP. Maybe in C ++ or Objective-C? - Andrew Kachalin
  • it turns out, for example, casting is also not an explicit method call? - Andrew Kachalin