我刚刚找到了一些 C++ 代码(位于http://msdn.microsoft.com/en-us/library/k8336763(VS.71).aspx),它使用了一种我以前从未见过的技术来添加类型一个现有的类:
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;
}
有人可以告诉我这种定义派生类型的技术叫什么,或者给我一些关于它的文档吗?我在使用 C++ 的 13 年中从未遇到过它,我想结束我的无知。