Appending ampersand to a class method

  ampersand, c++, methods

I came across some C++ code which looks roughly like this:

        void (classA::*methodA)() const noexcept
        {
            &classA::methodB
        };

Note: obviously I’m abstracting out the class name and method names in this example.

What is the purpose of methodA?

Analysis of the code

  1. It appears to be referencing methodB, but without parentheses, so it doesn’t appear to be calling the method
  2. It uses ampersand & which reads to me as take the address of
  3. Lastly methodA doesn’t return anything

Overall it appears to be just referencing the address of methodB.

Any suggestions as to what this is accomplishing?

Source: Windows Questions C++

LEAVE A COMMENT