请高手对下列模板详解一下!!!

ninyjun2008 2005-01-14 08:29:43
template <typename ClassT, typename FuncPtrT>
class sort_stub_ref_impl
{
public:

sort_stub_ref_impl(const ClassT& p, const FuncPtrT& f)
: m_p(p)
, m_f(f)
{}

template <typename T>
const bool
operator()(T *a, T *b) const
{
return (m_p.*m_f)(a, b); //尤其是这行代码
}

private:

const ClassT &m_p; //还有这行代码
FuncPtrT m_f;
};
...全文
108 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
fangrk 2005-01-14
  • 打赏
  • 举报
回复
#include <iostream>
#include <algorithm>
using namespace std;

class A
{
public:
static bool m_f(int * x,int * y)
{
return *x < *y;
}
};
template <typename FuncPtrT>
class sort_stub_ref_impl
{
public:

sort_stub_ref_impl(FuncPtrT f)
:m_f(f){}

template <typename T>
bool operator()(T *a, T *b) const
{
return (*m_f)(a, b);
}

private:
FuncPtrT m_f;
};

int main(int argc, char* argv[])
{
A a;
sort_stub_ref_impl< bool (*)(int*,int*) > xx(&A::m_f);
int i=12,j=34;
cout<<xx(&i,&j);
return 0;
}
ninyjun2008 2005-01-14
  • 打赏
  • 举报
回复
谢谢各位兄弟!买单!
oo 2005-01-14
  • 打赏
  • 举报
回复
sort_stub_ref_impl的m_f是一个ClassT的成员函数指针,跟ClassT里定义的名字无关,只要满足原型一样就可以了。
idler 2005-01-14
  • 打赏
  • 举报
回复
m_f是sort_stub_ref_impl的成员啊,m_p.m_f当然不行了
oo 2005-01-14
  • 打赏
  • 举报
回复
A的成员函数不一定要名字为m_f,只要原型一样就可以了。
oo 2005-01-14
  • 打赏
  • 举报
回复
楼主这么晚还做呀,呵呵...
ninyjun2008 2005-01-14
  • 打赏
  • 举报
回复
编译是通过了,但是有个新问题!!!! 见具体注解处

#include <iostream>
using namespace std;
//typedef bool (HELLO)(int*,int*);


class A
{
public:
int yy;

const bool m_f(int *,int *) const
{
cout << "hello" <<endl;
return false;
}
};
template <typename ClassT, typename FuncPtrT>
class sort_stub_ref_impl
{
public:

sort_stub_ref_impl(const ClassT& p, const FuncPtrT& f)
: m_p(p)
, m_f(f)
{}

template <typename T>
const bool
operator()(T *a, T *b) const
{
return (m_p.*m_f)(a, b); //既然调用的是m_p的成员为何需要定义m_f成员,而且没有该成员就编译不过。

}

private:

const ClassT &m_p;
FuncPtrT m_f;
};

typedef const bool (A::*HELLO)(int*,int*) const;


int main(int argc, char* argv[])
{
A a;
sort_stub_ref_impl<A,HELLO> xx(a,&A::m_f);
int i,j;
xx(&i,&j);
return 0;
}
//---------------------------------------------------------------------------
oo 2005-01-14
  • 打赏
  • 举报
回复
BTW:
test函数要放在A里面,做A的成员函数;
sort_stub_ref_impl<A,HELLO*> xx(a,test); 改成:sort_stub_ref_impl<A,HELLO*> xx(a,A::test);
ninyjun2008 2005-01-14
  • 打赏
  • 举报
回复
大哥还是编译不通过!!!!




#include <iostream>
using namespace std;
//typedef bool (HELLO)(int*,int*);


class A
{
public:
int yy;

bool m_f(int *,int *)
{
cout << "hello" <<endl;
return false;
}
};
template <typename ClassT, typename FuncPtrT>
class sort_stub_ref_impl
{
public:

sort_stub_ref_impl(const ClassT& p, const FuncPtrT& f)
: m_p(p)
, m_f(f)
{}

template <typename T>
const bool
operator()(T *a, T *b) const
{
return (m_p.*m_f)(a, b);

}

private:

const ClassT &m_p;
FuncPtrT m_f;
};

typedef bool (A::*HELLO)(int*,int*);


int main(int argc, char* argv[])
{
A a;
sort_stub_ref_impl<A,HELLO> xx(a,&A::m_f);
int i,j;
xx(&i,&j);
return 0;
}
oo 2005-01-14
  • 打赏
  • 举报
回复
m_f是ClassT成员函数指针,
原型是: bool function_name(T*,T*);
所以,你的HELLO不应该这样定义,typedef bool (HELLO)(int*,int*);
改成: typedef bool (A::*HELLO)(int*,int*); (放到A的定义后面定义这个)

WingForce 2005-01-14
  • 打赏
  • 举报
回复
typedef bool (*fun_ptr)(int *,int *);
struct A
{
fun_ptr m_f;
}

bool fun(int *a, int* b)
{
}

main()
{
A a;
a.m_f = fun;
//...
}
ninyjun2008 2005-01-14
  • 打赏
  • 举报
回复
所以说m_f不是m_p的成员函数
ninyjun2008 2005-01-14
  • 打赏
  • 举报
回复
按WingForce(初六,履霜,坚冰至。) 的说法,给m_p 定义了一个成员m_f



测试代码如下:
#include <iostream>
using namespace std;
typedef bool (HELLO)(int*,int*);

bool test(int *a,int* b)
{
cout << "hello" <<endl;
return false;
};
class A
{
public:
int yy;

bool m_f(int *,int *)
{
return false;
}
};
template <typename ClassT, typename FuncPtrT>
class sort_stub_ref_impl
{
public:

sort_stub_ref_impl(const ClassT& p, const FuncPtrT& f)
: m_p(p)
, m_f(f)
{}

template <typename T>
const bool
operator()(T *a, T *b) const
{
return (m_p.*m_f)(a, b); //编译不通过,但这段代码确实在一个软件中能用。
////改为 return (m_f)(a, b); 就可以

}

private:

const ClassT &m_p;
FuncPtrT m_f;
};

int main(int argc, char* argv[])
{
A a;
sort_stub_ref_impl<A,HELLO*> xx(a,test);
int i,j;
xx(&i,&j);
return 0;
}
//---------------------------------------------------------------------------
WingForce 2005-01-14
  • 打赏
  • 举报
回复
不对,m_f 是sort_stub_ref_impl的成员呀,并不是m_p的成员呀!
---------------------------
为什么m_p不能也有一个成员叫m_f?
ninyjun2008 2005-01-14
  • 打赏
  • 举报
回复
不对,m_f 是sort_stub_ref_impl的成员呀,并不是m_p的成员呀!
carylin 2005-01-14
  • 打赏
  • 举报
回复
return (m_p.*m_f)(a, b); //m_f是m_p的一个成员,是个函数指针,此处为调用m_p的成员
//函数指针指向的函数,将得到的结果作为返回值
const ClassT &m_p; //c++中的引用类型

64,670

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧