【问题标题】:Templated static member function pointer initialization模板化静态成员函数指针初始化
【发布时间】:2012-03-09 22:56:34
【问题描述】:
template<class K> 
class Cltest
{
public:
    static double (K::*fn)(double);
};

template<class K> 
double K::*Cltest<K>::fn(double) = NULL;

如何初始化静态成员函数指针?

【问题讨论】:

  • 我不确定,但你不能像初始化为0那样做吗?

标签: c++ templates initialization


【解决方案1】:

您需要将*fn 括在大括号中。
更正语法:

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

【讨论】:

  • 和往常一样,我迟到了 5 秒 ;)
【解决方案2】:

如果您使用适当的 typedef 来简化语法,那么这样做会容易得多:

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;

使用typedef,您实际上将函数的类型与变量名称分开。现在您可以分别查看它们,从而更容易理解代码。例如,上面的Cltest&lt;K&gt;::Fn 是类型,Cltest&lt;K&gt;::fn 是该类型的变量。

【讨论】:

    猜你喜欢
    • 2010-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-02
    • 2016-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多