在C++中,我可以在函数指针和函数引用之间进行选择(或者为了完整性,甚至是函数值):
void call_function_pointer (void (*function)()) {
(*function) ();
}
void call_function_reference (void (&function)()) {
function ();
}
void call_function_value (void function()) {
function ();
}
Run Code Online (Sandbox Code Playgroud)
然而,当谈到方法时,我似乎没有在指针和引用之间做出这种选择.
template <class T> void call_method_pointer (T* object, void (T::*method)()) {
(object->*method) ();
}
// the following code creates a compile error
template <class T> void call_method_reference (T& object, void (T::&method)()) {
object.method ();
}
Run Code Online (Sandbox Code Playgroud)
这使我假设C++中不存在方法引用.真的吗?如果是,他们不存在的原因是什么?