Why should I use an address operator to get a pointer to a member function?

struct A { void f() {} }; void f() {} int main() { auto p1 = &f; // ok auto p2 = f; // ok auto p3 = &A::f; // ok // // error : call to non-static member function // without an object argument // auto p4 = A::f; // Why not ok? } 

Why should I use the address operator to get a pointer to a member function?

+5
source share
1 answer
 auto p1 = &f; // ok auto p2 = f; // ok 

The first is more or less correct. But since non-member functions have implicit conversions to pointers, & not required. C ++ does this conversion, the same applies to static member functions.

For a quote from cppreference :

A value of a function of type T can be implicitly converted to a prvalue pointer to this function. This does not apply to a non-static member of a function, since lvalues ​​that reference non-static member functions do not exist.

+3
source

Source: https://habr.com/ru/post/1264049/


All Articles