您的位置:首页 > 编程语言 > C语言/C++

C++中几种类型的函数指针

2013-07-26 11:15 246 查看

1. 一般函数的指针定义

int foo(const char *out)
{
cout << "foo is called: " << out << endl;
return strlen(out);
}

typedef int (FooFunType)(const char*);         // 函数的类型别名
typedef int (FooFunPointerType)(const char*);  // 函数指针类型的别名

FooFunType* f1 = foo;
FooFunPointerType f2 = foo;

f1("f1-FooFunType");
f2("f2-FooFunPointerType");

// 输出为:
// foo is called: f1-FooFunType
// foo is called: f2-FooFunPointerType


2. 成员函数指针的定义

class Foo
{
public:
int Print(const char *out)
{
cout << "Foo::Print is called: " << out << endl;
}
};

typedef int (Foo::*FooMemPtr)(const char *out);

FooMemPtr clbkFooPrint = &Foo::Print;

Foo foo;
Foo *pFooObj = &foo;
pFooObj->*clbkFooPrint("class member function called.");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: