如何在数组初始化列表中初始化显式构造函数?

时间:2018-10-31 17:54:33

标签: c++ c++14

这是一个简单的问题...给了我一个带有显式构造函数的对象,我想在另一个构造函数中初始化它们的数组。这样做的语法是什么?

老实说,我一直在努力解决问题……而且我是一位经验丰富的C ++开发人员,这很尴尬!

class TestObj {
public:
  explicit TestObj(int i) {}
  TestObj(const TestObj&) = delete;
  TestObj& operator=(const TestObj&) = delete;
};

class AnotherObj {
public:
  // error: converting to ‘TestObj’ from initializer list would use explicit constructor ‘TestObj::TestObj(int)’
  AnotherObj(int i) : objs{{1}, {i}} {}

  // error: could not convert ‘i’ from ‘int’ to ‘TestObj’
  AnotherObj(int i, int j) : objs{i, j} {}

  // error: use of deleted function ‘TestObj::TestObj(const TestObj&)’
  AnotherObj(int i, int j, int k) : objs{TestObj{i}, TestObj{j}} {}

protected:
  TestObj objs[2];
};

0 个答案:

没有答案
相关问题