Kiran's Blog

Ramblings from a dehydrated mind…

  • Archives

  • Blog Stats

    • 9,866

Pointer to member functions

Posted by kiraninbng on July 10, 2006

A pointer to a member function consists of return type, the class name followed by ::, the function pointer name and the
function’s parameter list.

Ex :

class CTest
{
 int fn();
}

// pmCTest is a pointer to a member function which takes int as the parameter
int CTest::*pmCTest();
Below code shows how to invoke the pointer to member function,

pmCTest = &CTest::fn;
CTest oCTest;
CTest *pCTest = &oCTest;
// Invoke the member function using an object
(oCTest.*pmCTest)();
// Invoke the member function using an object pointer
(pCTest->*pmCTest)();

If you call virtual member function using a pointer to member function, the call will get resolved dynamically.

Also the size of the pointer to member function can change depending on whether the class is inherited from a single class
or from multiple classes.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>