Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are member functions addressed this way?

Tags:

c++

c++11

Why is there an ampersand when taking the address of a member function as opposed to a global function? I.e., why something like

std::bind(&MyClass::MemberFunction, ...);

when global functions would require

std::bind(GlobalFunction, ...);

?

like image 554
Kristian D'Amato Avatar asked Jan 24 '26 02:01

Kristian D'Amato


1 Answers

C allowed implicit conversions from global function names to pointers to those functions. C++ kept these implicit conversions for backwards compatibility reasons. C does not have member functions, and so there was no need to provide the implicit conversion in the case of member functions.

C++ does not allow the implicit conversion in the cases where it was not forced to for compatibility with C, because it was felt that such conversions were confusing and possibly ambiguous, while providing few benefits.

like image 175
Mankarse Avatar answered Jan 25 '26 17:01

Mankarse