0

I understand my second statement that "why & is not needed for normal function pointers" because function name itself is address of the function.

What I do not understand is why '&' is strictly needed for member function pointers?

Examples: Normal function pointers:

int add(int a, int b) {
  return (a + b);
}

int (*fp)(int, int);

fp = add;

(*fp)(2, 3) // This would give me addition of a and b, i.e. 5

Member function pointers:

class ABC {
  public:
    int i;
    ABC() { i = 0; }
    int addOne(int j) {
      return j + 1;
    }
};

// Member function pointer
int (ABC::*mfp)(int); 
// This is what I am talking about. '&' in below line.
mfp = &ABC::addOne;
ABC abc;
std::cout << (abc.*mfp)(2) << std::endl;
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Hemant Bhargava
  • 3,251
  • 4
  • 24
  • 45
  • you can only ever have ONE copy of a function in memory, so its address is fixed. but a method "name" can have MULTIPLE versions, e.g. a chain of inherited objects can each have their own "addOne" implementation, and you need to explicitly state WHICH of those many addOne's you want to use. – Marc B Oct 04 '16 at 14:56
  • You need to remove the C tag or this question will be down voted very quickly, just a reminder. – user3528438 Oct 04 '16 at 14:57
  • 1
    As far as I remember, according to Design and Evolution of C++, it was thought that it is better to explicitly indicate that you want a pointer, rather than call a function. To preserve backward compatibility, C++ still allows getting function pointers without &, but for member methods, which only exist in C++, it is mandated. – SergeyA Oct 04 '16 at 14:59
  • 4
    http://stackoverflow.com/questions/2795575/how-does-dereferencing-of-a-function-pointer-happen explains why C make that design decision. C++ just decide to do it the other way (more uniform and strict syntax over convenience of usage) for member functions while maintained C compatibility for regular function pointers. – user3528438 Oct 04 '16 at 15:09
  • @SergeyA `decltype` requires the `&` too, the most common use case being when using `std::unique_ptr` to arbitrarily call a function on destruction – Mgetz Oct 04 '16 at 16:50
  • The better question would be "why are we ever allowed to skip the & with regular functions". The answer is "to preserve a degree of compatibility with C". – n. m. could be an AI May 26 '19 at 04:53

1 Answers1

-1

It seems to me that the address-of operator is necessary for member function pointers because the right-hand side (rhs) of the declaration is a constant rather than a variable.

We wouldn't say

 int (ABC::*mfp)(int); 
 mfp = ABC::addOne();

because that would be to invoke a function.

Furthermore, the scope resolution operator :: has the highest precedence in the C++ table of operator precedence:

https://github.com/MicrosoftDocs/cpp-docs/blob/master/docs/cpp/cpp-built-in-operators-precedence-and-associativity.md

The :: operator is evaluated before any other operators are on the rhs. I suppose the complier wonders "Hmmmm...what's that? That should be a function, but..." and then sees the address-of operator and knows what the developer needs.

fishermanhat
  • 195
  • 1
  • 8