Initializing a Pointer to a Static Static Constant

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 a pointer to a static member function?

+2
source share
2 answers

You need to enclose *fnin curly braces.
Corrected syntax:

template<class K> 
double (K::*Cltest<K>::fn)(double) = 0;
+4
source

If you simplify the syntax with the appropriate typedef, it is much easier to do:

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 split the type of the function with the variable name. Now you can see them separately, which simplifies the understanding of the code. For example, Cltest<K>::Fnthere is a type above , and Cltest<K>::Fna variable of this type.

+4
source

Source: https://habr.com/ru/post/1669492/


All Articles