在类中存储 Typedef 构造函数

storing typedef constructor in class

本文关键字:Typedef 构造函数 存储      更新时间:2023-10-16

我正在尝试在我的类中存储一个typedef,我该怎么做?在发布的示例中,我想创建一个类,它允许我启动许多具有不同"Fct"参数的对象,例如"一个"或"斜率",甚至使用设置函数更改对象的"Fct f":

typedef double Fct(double);
double one(double x) { return 1; }
double slope(double x) { return x / 2; }
struct myFct : Shape {
    myFct(Fct f)
        :f(f) {}; //"f" is not a nonstatic data member of base class of class "myFct"
private:
    Fct f;
};

您的typedef代表函数类型。该typedef可用于声明成员函数。所以你的类有一个成员函数f声明。它接受双精度并返回双精度。

我怀疑你想要的是一个函数指针作为成员变量。明确地执行此操作:

struct myFct : Shape {
    myFct(Fct *f)
        :f(f) {}; //"f" is not a nonstatic data member of base class of class "myFct"
private:
    Fct *f;
};

您可能会认为自己很幸运,偶然发现了一个有点晦涩的功能。