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

成员函数与函数指针

2018年02月19日 ⁄ 综合 ⁄ 共 694字 ⁄ 字号 评论关闭
#include <iostream>
class test{
private:
	typedef int (test::*func)(int);
public:
	int getInt(int arg)
	{
		return arg;
	}
	int getChar(int arg)
	{
		return arg;
	}
	void Test()
	{
		func getd = &test::getInt;
		std::cout<<(this->*getd)(10)<<std::endl;
		getd = &test::getChar;
		std::cout<<((*this).*getd)('c')<<std::endl;
	}
	static void hello()
	{
		std::cout<<"hello,I am a static function."<<std::endl;
	}
};

int main()
{
	test ta;
	ta.Test();
	

	typedef int (test::*func)(int);
	func getD = &test::getInt;
	std::cout<<(ta.*getD)(10);
	std::cout<<((&ta)->*getD)(10);
	
	void (test::*tfunc)() = &test::Test;
	(ta.*tfunc)();
	((&ta)->*tfunc)();

	void (*hello)() = test::hello;
	hello();
	getchar();
	return 0;
}

 

 

操作符:operator->* 和.*;

Google gtest:http://www.cnblogs.com/coderzh/archive/2009/04/10/1432789.html

 

抱歉!评论已关闭.