template<class K>
class Cltest
{
public:
static double (K::*fn)(double);
};
template<class K>
double K::*Cltest<K>::fn(double) = NULL;
How can I initialize the static member function pointer?
template<class K>
class Cltest
{
public:
static double (K::*fn)(double);
};
template<class K>
double K::*Cltest<K>::fn(double) = NULL;
How can I initialize the static member function pointer?
You need to enclose *fn
in braces.
Corrected syntax:
template<class K>
double (K::*Cltest<K>::fn)(double) = 0;
If you simplify the syntax using appropriate typedef, then it is much easier to do that:
template<class K>
class Cltest
{
public:
typedef double (K::*Fn)(double); //use typedef
static Fn fn;
};
template<class K>
typename Cltest<K>::Fn Cltest<K>::fn = 0;
//Or you can initialize like this:
template<class K>
typename Cltest<K>::Fn Cltest<K>::fn = &K::SomeFun;
Using typedef
, you actually separate the type of the function from the variable name. Now you can see at them separately which makes it easier to understand the code. For example, above Cltest<K>::Fn
is the type and Cltest<K>::fn
is the variable of that type.