今天看某软件的源代码时,发现有这样一行

  1. typedef CPB_RETVAL (CPBprb_Prb::*CPBprb_action)(CPBprb_Prb &, unsigned);

其中CPB_RETVAL是一个enum类型,CPBprb_Prb是一个类,CPBprb_action在代码中其他地方没有找到定义。因不知这行代码所为何意,遂在论坛发贴请教 http://topic.csdn.net/u/20080908/11/0f127830-8c90-414d-9bab-5d946b115522.html?seed=1500702230

承蒙众多高手赐教,才知上段代码定义了成员函数指针 类型CPBprb_action, 该类型函数有2个参数,类型分别为CPBprb_Prb &, unsigned,返回值类型为CPB_RETVAL。此函数指针的类型有个定义域,它是类CPBprb_Prb的成员 (CPBprb_Prb::*CPBprb_action)。

另外, CPBprb_action不仅可以指向 CPBprb_Prb的成员函数,也可以指向CPBprb_Prb的派生类成员函数。甚至,CPBprb_Prb里可以根本就没有任何诸如CPB_RETVAL functionName (CPBprb_Prb &, unsigned)这样的函数,而完全可以在CPBprb_Prb的派生类里去增加 CPB_RETVAL functionName (CPBprb_Prb &, unsigned);这样的函数,如:

  1. #include <iostream>
  2. class A
  3. {
  4. public :
  5. A(){};
  6. virtual ~A(){};
  7. };
  8. class B : public A
  9. {
  10. public :
  11. B(){};
  12. ~B(){};
  13. int Fb( int a){ return a*a;}
  14. };
  15. typedef int (A::*Myfunction)( int );
  16. int main()
  17. {
  18. B* pB = new B;
  19. Myfunction pf = (Myfunction)(&B::Fb);
  20. int retValue = (pB->*pf)(3);
  21. std::cout<<retValue;
  22. std::cin>>retValue;
  23. return 1;
  24. }

通过typedef int (A::*Myfunction)(int);定义的Myfunction是A的成员(函数指针),但只能在类外定义。

需要指出的是,使用指向成员函数的指针变量是比较危险的!因此不建议这么用。

上文引用了论坛中对我帖子的回复,再次对关心并帮助我的人表示感谢。

PS:

函数指针(全局函数/类成员函数)和函数对象

(原文链接: http://www.ccrun.com/article.asp?i=1005&d=sc37og

关键字:函数指针 全局函数 类成员函数 函数对象 Function object

作者:ErranLi    更新:2007-01-06 02:31:47    浏览:9166

函数指针(全局函数/类成员函数)、函数对象(Function object)

一. 函数指针类型为全局函数.

#include "stdafx.h"

#include <iostream>
using namespace std;

class TestAction;

typedef void (*fp)( int );

void Drink( int i)
{
cout<< "No. " <<i<< " drink..." <<endl;
}

void Eat( int i)
{
cout<< "No. " <<i<< " eat..." <<endl;
}

class TestAction
{
public :
fp testAct;

void TestAct( int i)
{
if (testAct != NULL)
{
testAct(i);
}
}
};

int main( int argc, char * argv[])
{
TestAction doact;
doact.testAct = &Drink;
doact.TestAct(0);
doact.TestAct(1);
doact.TestAct(2);
doact.testAct = &Eat;
doact.TestAct(0);
doact.TestAct(1);
doact.TestAct(2);
return 0;
}

二. 函数指针类型为类成员函数.

#include "stdafx.h"

#include <iostream>
using namespace std;

class Action;
class TestAction;

// 函数指针类型为类 Action 的成员函数
typedef void (Action::*fp)( int );

class Action
{
public :
void Drink( int i)
{
cout<< "No. " <<i<< " drink..." <<endl;
}

void Eat( int i)
{
cout<< "No. " <<i<< " eat..." <<endl;
}
};

class TestAction
{
public :
// 定义一个函数指针
fp testAct;
//Action 对象实例 , 该指针用于记录被实例化的 Action 对象
Action * pAction;

void TestAct( int i)
{
if ((pAction != NULL) && (testAct != NULL))
{
// 调用
(pAction->*testAct)(i);
}
}
};

int main( int argc, char * argv[])
{
Action act;
TestAction doact;
doact.pAction = &act;
doact.testAct = &Action::Drink;
doact.TestAct(0);
doact.TestAct(1);
doact.TestAct(2);
doact.testAct = &Action::Eat;
doact.TestAct(0);
doact.TestAct(1);
doact.TestAct(2);
return 0;
}

三. 函数对象 (Function object)
// 本文转自 C++Builder研究 - http://www.ccrun.com/article.asp?i=1005&d=sc37og

#include "stdafx.h"

#include <iostream>
#include <functional>

using namespace std;

class Action;
class Drink;
class Eat;
class TestAction;

class Action
{
public :
int operator ()( int i)
{
Act(i);
return i;
}

virtual void Act( int i) = 0;
};

class Drink : public Action
{
void Act( int i)
{
cout<< "No. " <<i<< " drink..." <<endl;
}
};

class Eat : public Action
{
void Act( int i)
{
cout<< "No. " <<i<< " eat..." <<endl;
}
};

class TestAction
{
public :
void TestAct( int i, Action& testAct)
{
testAct(i);
}
};

int main( int argc, char * argv[])
{
TestAction doact;
doact.TestAct(0, Drink());
doact.TestAct(1, Drink());
doact.TestAct(2, Drink());
doact.TestAct(0, Eat());
doact.TestAct(1, Eat());
doact.TestAct(2, Eat());
return 0;
}

虽然传递函数指针被广泛应用于事件驱动系统中,以此实现回调函数通过指针来调用。但 C++ 还是提供了另外一种可供选择的办法,即函数对象,利用它可以
避免使用函数指针。这样做有几个优点。首先, 因为对象可以在内部修改而不用改动外部接口,因此设计更灵活,更富有弹性。函数对象也具备有存储先前调用结
果的数据成员。。 此外,编译器可以内联函数对象,从而进一步增强性能。函数对象可以具体表达依赖成员模板的通用算法 , 这些算法借助普通的函数指针难
以完成。例用函数对象实现了一个通用的 Negation 算法操作:

#include "stdafx.h"
#include <iostream>

using namespace std;

class Negate
{
public :
template < class T> T operator ()(T t) const
{
return -t;
}
};

void Callback( int n, const Negate& neg) // 传递一个函数对象
{
n = neg(n); // 调用重载的 () 操作 来对 n 进行 negate 操作
cout << n << endl;
}

int main( int argc, char * argv[])
{
// 调用方式一
Callback(5, Negate());

// 调用方式二
Negate neg;
cout << neg(9.99999) << endl;
cout << neg( __int32 (39999999)) << endl;

return 0;
}

STL 库中定义了很多函数对象以供相关算法调用,如 模板化的函数对象 greater<> 或者 less<>:

vector < int > vi;
//.. 填充向量
sort(vi.begin(), vi.end(), greater< int >() ); // 降序 (descending)
sort(vi.begin(), vi.end(), less< int >() ); // 升序 (ascending)

本文转自:http://www.cppblog.com/erran/archive/2006/12/26/16868.html

函数对象(function
object),又名仿函数(functor),是重载了operator()的一个类,其实在stl算法中有大量运用,c++标准预定义的如
less <>,greater <>,bind2nd <>等
为什么引入仿函数?
原因:
1.仿函数有具体型别,可用在模板参数,而函数不行
2.同类仿函数可有不同植,可代表不同状态,比函数灵活(须定义多个)
3.仿函数可能比普通函数快
........可能还有,不过凭印象就这些了