- 論壇徽章:
- 14
|
回復(fù) 2# Moon_Bird
我給他寫(xiě)個(gè)例子吧,容易理解些- #include <iostream>
- using namespace std;
- struct foo
- {
- int bar;
- int baz;
- int qux( int v ) const
- {
- return bar+baz+v;
- }
-
- foo() : bar(1), baz(2)
- {
- }
- };
- void fun1( foo& a, int foo::* memvar )
- {
- cout << a.*memvar << endl;
- }
- void fun2( foo& a, int (foo::*memfun)(int v) const )
- {
- cout << (a.*memfun)(3) << endl;
- }
- int main( void )
- {
- foo a;
- fun1( a, &foo::bar ); // output 1
- fun1( a, &foo::baz ); // output 2
- fun2( a, &foo::qux ); // output 6
- return 0;
- }
復(fù)制代碼 |
|