在类中存储 Typedef 构造函数
storing typedef constructor in class
我正在尝试在我的类中存储一个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;
};
您可能会认为自己很幸运,偶然发现了一个有点晦涩的功能。
相关文章:
- "error: no matching function for call to"构造函数错误
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 为什么在C++中使用私有复制构造函数与删除复制构造函数
- 选择要调用的构造函数
- 如何委托派生类使用其父构造函数?
- 构造函数正在调用一个使用当前类类型的函数
- 没有用于初始化C++中的变量模板的匹配构造函数
- 初始化具有非默认构造函数的std::数组项的更好方法
- 在类中存储 Typedef 构造函数
- 使typedef的构造函数/赋值运算符成为友元函数
- 正在调用模板内typedef的构造函数
- 带有指针和构造函数的 typedef 结构
- Typedef是一个具有特定构造函数参数的类
- 如何使用包含不同typedef枚举的派生类模板设置构造函数初始化列表
- C++标准是否允许使用typedef来重命名构造函数
- 将typedef与构造函数参数一起使用
- 如何使用默认构造函数参数对模板进行typedef
- "using" (typedef) 指针:它的默认构造函数返回 nullptr?