基类成员函数指针使用
2010-12-30 17:03
204 查看
#include <iostream>
#include <string>
#include <functional>
using namespace std;
class Test
{
public:
Test()
{
}
Test(const string& str)
:s(str)
{
}
public:
int output()
{
cout<<s<<endl;
return 0;
}
private:
string s;
};
template <typename T, typename R>
class my_men_func_t
:public unary_function<T, R>
{
private:
R (T::*pmf) ();//声明以T为基类的成员函数指针,并返回类型为R
public:
explicit my_men_func_t(R(T::*p)())//需要显示构造以p为函数指针对象。
:pmf(p)
{
}
public:
R print(T& X)//成员函数
{
return ((X.*pmf)());//调用基类的成员函数
}
};
int main(int argc, char **argv)
{
my_men_func_t<Test, int> m(&Test::output);//以基类的函数地址为参数来定义一个对象
Test t("123");
m.print(t);//调用成员函数
return 0;
}
#include <string>
#include <functional>
using namespace std;
class Test
{
public:
Test()
{
}
Test(const string& str)
:s(str)
{
}
public:
int output()
{
cout<<s<<endl;
return 0;
}
private:
string s;
};
template <typename T, typename R>
class my_men_func_t
:public unary_function<T, R>
{
private:
R (T::*pmf) ();//声明以T为基类的成员函数指针,并返回类型为R
public:
explicit my_men_func_t(R(T::*p)())//需要显示构造以p为函数指针对象。
:pmf(p)
{
}
public:
R print(T& X)//成员函数
{
return ((X.*pmf)());//调用基类的成员函数
}
};
int main(int argc, char **argv)
{
my_men_func_t<Test, int> m(&Test::output);//以基类的函数地址为参数来定义一个对象
Test t("123");
m.print(t);//调用成员函数
return 0;
}
相关文章推荐
- 类成员函数指针的使用
- 一个完整的使用成员函数指针的例子
- “Caculate::writehtml”: 函数调用缺少参数列表;请使用“&Caculate::writehtml”创建指向成员的指针
- boost准模板库date迭代器(指针)使用以及常用的两个静态成员函数is_leap_year()和end_of_month_day()
- 类的成员函数指针的使用方法
- 如何得到类成员函数的指针(操作符.×和->*的使用)
- 如何使用c++的成员函数指针
- 成员函数指针使用
- 使用std::function 把类成员函数指针转换为普通函数指针
- 成员函数指针的使用
- 在模板类成员函数入参中使用回调函数指针
- static函数 函数调用缺少参数列表;请使用“&XXX”创建指向成员的指针;类的static成员函数
- error C3867: “A::fun1”: 函数调用缺少参数列表;请使用“&A::fun1”创建指向成员的指针
- 类里面的成员函数指针使用
- 用对象和用指向基类的指针访问成员函数
- 函数调用缺少参数列表;请使用“&****Dlg::getThread”创建指向成员的指针”--多线程函数调用
- 基类指针使用重载输入输出运算符函数操作派生类
- C++成员函数指针的使用范例
- 第十七章 特殊成员_使用typedef简化函数指针的声明
- error: 函数调用缺少参数列表;请使用“&BinTree<char>::_PrintNode”创建指向成员的指针