现在的位置: 首页 > 综合 > 正文

How to use the pointer of function in a class?

2014年07月24日 ⁄ 综合 ⁄ 共 421字 ⁄ 字号 评论关闭

Let us see the example first.

#include <iostream>
using namespace std;

class A {
public:
	A (int func(int, int)) { compare = func; }  // give value
	void Test (int a, int b) { cout<<compare(a, b)<<endl; }  // wrapper
private:
	int (*compare) (int, int);  // pointer of function
};

int function (int a, int b) {
	return (a-b);
}

int main()
{
	A a(function);
	a.Test(1,2);
	return 0;
}

Obviously, we can see the pointer of function in the A class and give the value in the construction. Note the argument in the construction.

抱歉!评论已关闭.