为什么在使用大括号初始化器时继承的构造函数不起作用? (C ++)

时间:2018-10-04 11:56:08

标签: c++

我有一组这样的类:

class Member {
public:
  Member(int val) : val_(val) {}
private:
  int val_;
};

class A {
public:
  A(const Member& member) : member_(member) {}

private:
  Member member_;
};

class B : public A {
  using A::A;
private:
  Member other_member_{10}; // <--- commenting this line fixes errors
};

class C : public B {
public:
  C(const Member& member) : B(member) {}
};

当我尝试构造C类的对象时:

int main(int argc, char** argv) {
  Member member(12);
  C c(member);
  return 0;
}

我收到以下错误:

~/work/src/test/main.cpp: In constructor ‘C::C(const Member&)’:                                                                      
~/work/src/test/main.cpp:26:37: error: use of deleted function ‘B::B(const Member&)’                                                 
   C(const Member& member) : B(member) {}                                                                                                      
                                     ^                                                                                                         
~/work/src/test/main.cpp:19:12: note: ‘B::B(const Member&)’ is implicitly deleted because the default definition would be ill-formed:
   using A::A;                                                                                                                                 
            ^                                                                                                                                  
~/work/src/test/main.cpp:19:12: error: no matching function for call to ‘Member::Member()’                                           
some notes with candidates   

我知道using A::A应该为所有基本构造函数提供可见性,但是如错误消息中所述,它已被隐式删除...

那么,为什么在将带有brace-initializer的成员添加到B类之后,却出现这些错误?

谢谢。

0 个答案:

没有答案