C++;,指向函数作为新类型的指针 在C++ 2003中,TyPIFF只能用于完整的类型。因此,不允许创建指向函数和泛型T的指针作为类型: template <typename T> typedef T(*f_function)(T, T, T);
我想使用指向函数fff的指针作为类成员C++;,指向函数作为新类型的指针 在C++ 2003中,TyPIFF只能用于完整的类型。因此,不允许创建指向函数和泛型T的指针作为类型: template <typename T> typedef T(*f_function)(T, T, T);,c++,parameter-passing,function-pointers,generic-programming,C++,Parameter Passing,Function Pointers,Generic Programming,我想使用指向函数fff的指针作为类成员 template <typename T> class A { public: f_function fff; A() {fff = NULL;} A( f_function pf){fff = &pf;} }; template <typename T> T f1(T x, T y, T z) { return x + y + z;} template <typename T>
template <typename T>
class A
{
public:
f_function fff;
A() {fff = NULL;}
A( f_function pf){fff = &pf;}
};
template <typename T>
T f1(T x, T y, T z) { return x + y + z;}
template <typename T>
T f2(T x, T y, T z) { return x - y - z;}
模板
甲级
{
公众:
f_函数fff;
A(){fff=NULL;}
(f_函数pf){fff=&pf;}
};
模板
f1(tx,tyt,tz){返回x+y+z;}
模板
f2(tx,tyz,tz){返回x-y-z;}
并在构造函数中初始化其值。随后:
int main()
{
A <double> a(f1);
double res = a.getX(1.1, 2.2, 3.3);
}
intmain()
{
A(f1);
双精度=a.getX(1.1,2.2,3.3);
}
这个建筑安全吗?还有别的办法解决这个问题吗?感谢您的帮助。您可以使用别名模板(C++11)声明:
template <typename T>
using f_function = T(*)(T, T, T);
模板
使用f_函数=T(*)(T,T,T);
例如:
#包括
#包括
#包括
模板
使用f_函数=T(*)(T,T,T);
模板
甲级
{
公众:
f_函数fff;
A(){fff=NULL;}
(f_函数pf){fff=pf;}
};
模板
f1(tx,tyt,tz){返回x+y+z;}
模板
f2(tx,tyz,tz){返回x-y-z;}
int main()
{
A(f1);
双分辨率=a.fff(1.1,2.2,3.3);
std::我很确定C++03允许typedef转换为不完整的类型:struct X;typedef X Y;
template <typename T>
using f_function = T(*)(T, T, T);
#include <iostream>
#include <string>
#include <array>
template <typename T>
using f_function = T(*)(T, T, T);
template <typename T>
class A
{
public:
f_function<T> fff;
A() {fff = NULL;}
A( f_function<T> pf){fff = pf;}
};
template <typename T>
T f1(T x, T y, T z) { return x + y + z;}
template <typename T>
T f2(T x, T y, T z) { return x - y - z;}
int main()
{
A <double> a(f1);
double res = a.fff(1.1, 2.2, 3.3);
std::cout << res;
}