Method pointer
A method pointer is used in object-oriented programming to refer to a method of a class or an object instance . It is similar to a function pointer (a pointer to a function ). In some programming languages, it is common to refer to a method pointer as a method or a reference to a method . A distinction is made between bound and unbound method pointers, depending on when the self or this parameter is determined.
Bound method pointers
With bound methods ( bound method in Python ) the method pointer contains a pointer to a method and a pointer to an object instance. Such method pointer can be used as callback function (callback function) use to actions at events (ger .: events) trigger.
Unbound method pointers
In the case of unbound (also: free) methods ( unbound method in Python, method in Java ), the method pointer contains a reference to a method, but not a reference to an object instance. A suitable object instance must be transferred to an unbound method pointer when it is called.
There are also unbound method pointers in C ++ . These point to a method of a class and are bound to an object when called. The operators .*
and exist for this ->*
. Internally, a method pointer is a number that references an entry in the method table of the class.
Also, Objective-C provides unbound method pointer type IMP
. Due to their lack of object orientation, however, they are rarely used to improve runtime behavior. Otherwise the superior concept of selectors is used.
Example in C ++
#include <iostream>
class Auto {
public:
// 4 "member functions" = "Methoden"
void starten() {
std::cout << "Starten..." << std::endl;
}
void beschleunigen(double beschleunigung);
void bremsen(double verzoegerung);
void ausschalten();
};
int main() {
Auto a;
// erzeuge method_ptr und initialisiere ihn mit Auto::starten
void (Auto::*method_ptr)() = &Auto::starten;
// rufe Methode, auf die method_ptr zeigt. in dem Falle: a.starten()
(a.*method_ptr)();
// FEHLER: Falscher Methodentyp, da Auto::beschleunigen einen Parameter
// erwartet
method_ptr = &Auto::beschleunigen;
return 0;
}
Related concepts
Delegates
In .NET languages such as C # , VB.NET, or D , delegate variables are used to create type-safe method pointers. In addition, .NET delegates support other operations.
Delegates in late binding languages
In late binding languages such as Objective-C, a distinction must be made between the message (“method call”) and the method. The compiler does not translate a method and its call into an index for a vtab . Rather, the message remains unchanged. The list of methods of an object is an associative array, i.e. it contains a reference from the message name to the method. It is therefore possible to decide which method should be executed based on the messages only at runtime. As a result, it is not necessary to know the class of the recipient or that the method is already at least abstractly defined in a known superclass.
// Instanz beliebiger (unbekannter) Klasse
id myDelegate;
// Nachrichtenname
SEL messageSelector = @selector(sender:doSomethingWithObject:);
if ([myDelegate respondsToSelector:messageSelector]) {
[myDelegate sender:self doSomethingWithObject:anObject];
}
Please note that by using id, any instance of any class can be accepted as a recipient. Since delegates do not have to implement all methods, a query is made at runtime whether a corresponding implementation is available. So delegating is an offer, not an obligation. It serves to avoid derivations (white boxing) and to create a defined API for functional specialization. On the implementation side, the existence of a method implementation can be faked and a message can be forwarded in this way (forward invocation, proxy).
Web links
- Pointers to member functions, C ++ FAQ (English) - 15 theoretically and practically sound articles on method pointers