我有一个指向通用静态方法的指针
class MyClass
{
private:
static double ( *pfunction ) ( const Object *, const Object *);
...
};指向静态方法
class SomeClass
{
public:
static double getA ( const Object *o1, const Object *o2);
...
};初始化:
double ( *MyClass::pfunction ) ( const Object *o1, const Object *o2 ) = &SomeClass::getA;我想将此指针转换为静态模板函数指针:
template <class T>
static T ( *pfunction ) ( const Object <T> *, const Object <T> *); //Compile error其中:
class SomeClass
{
public:
template <class T>
static double getA ( const Object <T> *o1, const Object <T> *o2);
...
};但是有以下编译错误:
error: template declaration of : T (* pfunction )(const Object <T> *o1, const Object <T> *o2)谢谢你的帮助。
发布于 2011-01-01 21:17:13
template是一个模板 :)它不是一个具体的类型,不能用作成员。例如,您不能定义以下类:
class A
{
template <class T> std::vector<T> member;
}因为template <class T> std::vector<T> member;是潜在地可以专用于许多不同类型的东西。你可以这样做:
template <class T>
struct A
{
static T (*pfunction)();
};
struct B
{
template <class T>
static T getT();
};
int (*A<int>::pfunction)() = &B::getT<int>;这里的A<int>是一个专门的模板,因此也有专门的成员
https://stackoverflow.com/questions/4573941
复制相似问题