在 class 中存储 typedef 构造函数

storing typedef constructor in class

我正在尝试在我的 class 中存储一个 typedef,我该怎么做?在发布的示例中,我想创建一个 class,它允许我使用不同的 "Fct" 参数启动多个对象,例如 "one" 或 "slope",甚至更改一个对象的 "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 可以用来声明成员函数。所以你的 class 有一个 成员函数 f 声明。它接受双精度和 returns 双精度。

我怀疑你想要的是作为成员变量的函数指针。明确地做:

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

private:
    Fct *f;
};

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