fig21_13.cpp

来自「经典vc教程的例子程序」· C++ 代码 · 共 35 行

CPP
35
字号
// Fig. 21.13 fig21_13.cpp
// Demonstrating operators .* and ->*
#include <iostream.h>

class Test {
public:
   void function() { cout << "function\n"; }
   int value;
};

void arrowStar( Test * );
void dotStar( Test * );

int main()
{
   Test t;    
  
   t.value = 8;
   arrowStar( &t );
   dotStar( &t );
   return 0; 
}

void arrowStar( Test *tPtr )
{
   void ( Test::*memPtr )() = &Test::function;
   ( tPtr->*memPtr )();  // invoke function indirectly
}

void dotStar( Test *tPtr )
{
   int Test::*vPtr = &Test::value;
   cout << ( *tPtr ).*vPtr << endl;  // access value   
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?