一般函数指针和类的成员函数指针——其实是调用约定惹的事
2012-10-17 10:43
344 查看
转自:http://www.cnblogs.com/xianyunhe/archive/2011/11/26/2264709.html
函数指针是通过指向函数的指针间接调用函数。函数指针可以实现对参数类型、参数顺序、返回值都相同的函数进行封装,是多态的一种实现方式。由于类的非静态成员函数中有一个隐形的this指针,因此,类的成员函数的指针和一般函数的指针的表现形式不一样。
1、指向一般函数的指针
函数指针的声明中就包括了函数的参数类型、顺序和返回值,只能把相匹配的函数地址赋值给函数指针。为了封装同类型的函数,可以把函数指针作为通用接口函数的参数,并通过函数指针来间接调用所封装的函数。
下面是一个指向函数的指针使用的例子。
#include <iostream.h>
/*指向函数的指针*/
typedef int (*pFun)(int, int);
int Max(int a, int b)
{
return a > b ? a : b;
}
int Min(int a, int b)
{
return a < b ? a : b;
}
/*通用接口函数,实现对其他函数的封装*/
int Result(pFun fun, int a, int b)
{
return (*fun)(a, b);
}
void main()
{
int a = 3;
int b = 4;
cout<<"Test function pointer: "<<endl;
cout<<"The maximum number between a and b is "<<Result(Max, a, b)<<endl;
cout<<"The minimum number between a and b is "<<Result(Min, a, b)<<endl;
}
2、指向类的成员函数的指针
类的静态成员函数采用与一般函数指针相同的调用方式,而受this指针的影响,类的非静态成员函数与一般函数指针是不兼容的。而且,不同类的this指针是不一样的,因此,指向不同类的非静态成员函数的指针也是不兼容的。指向类的非静态成员函数的指针,在声明时就需要添加类名。
下面是一个指向类的成员函数的指针的使用的例子,包括指向静态和非静态成员函数的指针的使用。
#include <iostream.h>
class CA;
/*指向类的非静态成员函数的指针*/
typedef int (CA::*pClassFun)(int, int);
/*指向一般函数的指针*/
typedef int (*pGeneralFun)(int, int);
class CA
{
public:
int Max(int a, int b)
{
return a > b ? a : b;
}
int Min(int a, int b)
{
return a < b ? a : b;
}
static int Sum(int a, int b)
{
return a + b;
}
/*类内部的接口函数,实现对类的非静态成员函数的封装*/
int Result(pClassFun fun, int a, int b)
{
return (this->*fun)(a, b);
}
};
/*类外部的接口函数,实现对类的非静态成员函数的封装*/
int Result(CA* pA, pClassFun fun, int a, int b)
{
return (pA->*fun)(a, b);
}
/*类外部的接口函数,实现对类的静态成员函数的封装*/
int GeneralResult(pGeneralFun fun, int a, int b)
{
return (*fun)(a, b);
}
void main()
{
CA ca;
int a = 3;
int b = 4;
cout<<"Test nonstatic member function pointer from member function:"<<endl;
cout<<"The maximum number between a and b is "<<ca.Result(CA::Max, a, b)<<endl;
cout<<"The minimum number between a and b is "<<ca.Result(CA::Min, a, b)<<endl;
cout<<endl;
cout<<"Test nonstatic member function pointer from external function:"<<endl;
cout<<"The maximum number between a and b is "<<Result(&ca, CA::Max, a, b)<<endl;
cout<<"The minimum number between a and b is "<<Result(&ca, CA::Min, a, b)<<endl;
cout<<endl;
cout<<"Test static member function pointer: "<<endl;
cout<<"The sum of a and b is "<<GeneralResult(CA::Sum, a, b)<<endl;
}
函数指针是通过指向函数的指针间接调用函数。函数指针可以实现对参数类型、参数顺序、返回值都相同的函数进行封装,是多态的一种实现方式。由于类的非静态成员函数中有一个隐形的this指针,因此,类的成员函数的指针和一般函数的指针的表现形式不一样。
1、指向一般函数的指针
函数指针的声明中就包括了函数的参数类型、顺序和返回值,只能把相匹配的函数地址赋值给函数指针。为了封装同类型的函数,可以把函数指针作为通用接口函数的参数,并通过函数指针来间接调用所封装的函数。
下面是一个指向函数的指针使用的例子。
#include <iostream.h>
/*指向函数的指针*/
typedef int (*pFun)(int, int);
int Max(int a, int b)
{
return a > b ? a : b;
}
int Min(int a, int b)
{
return a < b ? a : b;
}
/*通用接口函数,实现对其他函数的封装*/
int Result(pFun fun, int a, int b)
{
return (*fun)(a, b);
}
void main()
{
int a = 3;
int b = 4;
cout<<"Test function pointer: "<<endl;
cout<<"The maximum number between a and b is "<<Result(Max, a, b)<<endl;
cout<<"The minimum number between a and b is "<<Result(Min, a, b)<<endl;
}
2、指向类的成员函数的指针
类的静态成员函数采用与一般函数指针相同的调用方式,而受this指针的影响,类的非静态成员函数与一般函数指针是不兼容的。而且,不同类的this指针是不一样的,因此,指向不同类的非静态成员函数的指针也是不兼容的。指向类的非静态成员函数的指针,在声明时就需要添加类名。
下面是一个指向类的成员函数的指针的使用的例子,包括指向静态和非静态成员函数的指针的使用。
#include <iostream.h>
class CA;
/*指向类的非静态成员函数的指针*/
typedef int (CA::*pClassFun)(int, int);
/*指向一般函数的指针*/
typedef int (*pGeneralFun)(int, int);
class CA
{
public:
int Max(int a, int b)
{
return a > b ? a : b;
}
int Min(int a, int b)
{
return a < b ? a : b;
}
static int Sum(int a, int b)
{
return a + b;
}
/*类内部的接口函数,实现对类的非静态成员函数的封装*/
int Result(pClassFun fun, int a, int b)
{
return (this->*fun)(a, b);
}
};
/*类外部的接口函数,实现对类的非静态成员函数的封装*/
int Result(CA* pA, pClassFun fun, int a, int b)
{
return (pA->*fun)(a, b);
}
/*类外部的接口函数,实现对类的静态成员函数的封装*/
int GeneralResult(pGeneralFun fun, int a, int b)
{
return (*fun)(a, b);
}
void main()
{
CA ca;
int a = 3;
int b = 4;
cout<<"Test nonstatic member function pointer from member function:"<<endl;
cout<<"The maximum number between a and b is "<<ca.Result(CA::Max, a, b)<<endl;
cout<<"The minimum number between a and b is "<<ca.Result(CA::Min, a, b)<<endl;
cout<<endl;
cout<<"Test nonstatic member function pointer from external function:"<<endl;
cout<<"The maximum number between a and b is "<<Result(&ca, CA::Max, a, b)<<endl;
cout<<"The minimum number between a and b is "<<Result(&ca, CA::Min, a, b)<<endl;
cout<<endl;
cout<<"Test static member function pointer: "<<endl;
cout<<"The sum of a and b is "<<GeneralResult(CA::Sum, a, b)<<endl;
}
相关文章推荐
- C++成员函数 this指针 调用约定
- 成员函数指针和其他类型的强制转换,使用一般指针调用成员函数
- C++指针直接调用类成员函数探讨(转载)
- 一般函数指针和类的成员函数指针
- 空指针也能调用成员函数?
- 一般函数指针和类的成员函数指针
- 浅析C++中的this指针 通过空指针(NULL)可以正确调用一些类的成员函数?
- 一般函数指针和类的成员函数指针
- 空类指针调用成员函数 类内的this指针
- c++ 如何把this指针传入成员函数 像全局函数一样调用成员函数
- 一般函数指针和类的成员函数指针
- C/C++中为什么在类外利用多态基类指向派生类指针可以调用类的私有成员函数?
- 为什么通过空指针(NULL)可以正确调用一些类的成员函数?
- 空的类指针照样调用成员函数
- this 指针的地址--调用成员函数的所在对象的起始地址
- error C3867: “A::fun1”: 函数调用缺少参数列表;请使用“&A::fun1”创建指向成员的指针
- C++中通过派生类调用第二基类的普通成员函数时this指针的调整
- 函数调用缺少参数列表;请使用“&Student::Printf”创建指向成员的指针 问题解析
- 一般函数指针与成员函数指针
- 空指针为什么能调用成员函数?(转载)