c++ - (Re)named std::pair 成员中断初始化

标签 c++ c++11 subclassing std-pair

有以下类型:

typedef pair<double, double> MinMax; ///< first - Min, second - Max

并且用它初始化工作正常:

const MinMax mInMinMax[FunctionCount] = {{-1, 1}, {-1, 1}, {0, 1}};

但是,如果为了方便我将 pair 子类化:

///< first - Min, second - Max
struct MinMax : public pair<double, double>
{
    double& Min() { return first; }
    double Min() const { return first; }
    double& Max() { return second; }
    double Max() const { return second; }
};

编译失败并出现错误:

error: could not convert ‘{-1, 1}’ from ‘<brace-enclosed initializer list>’ to ‘const MinMax’ const MinMax mInMinMax[FunctionCount] = {{-1, 1}, {-1, 1}, {0, 1}};

是否可以子类化 pair<double, double>正确吗?

最佳答案

基类的构造函数不是自动“继承”的。 您的 Min/Max 函数与此问题无关:

struct MinMax : public pair<double, double>
{
   using pair<double, double>::pair;
   ...
};

关于c++ - (Re)named std::pair 成员中断初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44009255/

相关文章:

c++ - 为什么整数溢出会导致 C++ iostream 出错?

c++ - 为什么调用这个复制构造函数而不是移动构造函数?

c++ - 通过传递指针的地址来初始化 std::unique_ptr

ios - 自定义 UINavigationBar sizeThatFits 实现不返回真实的 frame.size.width

c++ - "making"unique_ptr<T> 隐式转换为 T* 的陷阱?

c++ - 分析 Windows : what does the error message tell us? 中的崩溃

c++ - 句柄无效(使用应用程序验证程序)

c++ - 如何在 Visual Basic 中使用矩形函数 OpenGL 绘制矩形

c++ - 为什么以及如何在 C++ 中使用 bind() 作为谓词?

Swift 从父类(super class)调用子类的重写方法