Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is this C++ technique for adding types to a class called?

I've just found some C++ code (at http://msdn.microsoft.com/en-us/library/k8336763(VS.71).aspx), which uses a technique I've never seen before to add types to an existing class:

class Testpm {
public:
   void m_func1() { cout << "m_func1\n"; }
   int m_num;
};

// Define derived types pmfn and pmd.
// These types are pointers to members m_func1() and m_num, respectively.
void (Testpm::*pmfn)() = &Testpm::m_func1;
int Testpm::*pmd = &Testpm::m_num;

int main() {
   Testpm ATestpm;
   Testpm *pTestpm = new Testpm;

   // Access the member function
   (ATestpm.*pmfn)();
   (pTestpm->*pmfn)();   // Parentheses required since * binds

   // Access the member data
   ATestpm.*pmd = 1;
   pTestpm->*pmd = 2;

   cout  << ATestpm.*pmd << endl
         << pTestpm->*pmd << endl;
}

Can someone please tell me what this technique for defining derived types is called, or point me to some documentation on it? I've never come across it in 13 years of using C++, and would like to end my ignorance.

like image 656
Charles Anderson Avatar asked Dec 29 '22 12:12

Charles Anderson


1 Answers

The comment is incorrect: pmfn and pmd are not "derived types" at all (they are not even types!). They are pointers to members.

like image 52
user200783 Avatar answered Jan 25 '23 23:01

user200783