ChatGPT解决这个技术问题 Extra ChatGPT

继承构造函数

为什么这段代码:

class A
{
    public: 
        explicit A(int x) {}
};

class B: public A
{
};

int main(void)
{
    B *b = new B(5);
    delete b;
}

导致这些错误:

main.cpp: In function ‘int main()’:
main.cpp:13: error: no matching function for call to ‘B::B(int)’
main.cpp:8: note: candidates are: B::B()
main.cpp:8: note:                 B::B(const B&)

B不应该继承A的构造函数吗?

(这是使用 gcc)


A
Antonio

如果您的编译器支持 C++11 标准,则存在使用 using 的构造函数继承(双关语)。有关更多信息,请参阅 Wikipedia C++11 article。你写:

class A
{
    public: 
        explicit A(int x) {}
};

class B: public A
{
     using A::A;
};

这是全有或全无 - 你不能只继承一些构造函数,如果你写这个,你继承所有的。要仅继承选定的那些,您需要手动编写各个构造函数并根据需要从它们调用基本构造函数。

从历史上看,构造函数不能在 C++03 标准中继承。您需要通过自己调用基本实现来手动继承它们。

对于模板化基类,请参考以下示例:

using std::vector;
    
template<class T>
class my_vector : public vector<T> {
    public:
    using vector<T>::vector; ///Takes all vector's constructors
    /* */
};

这是非常邪恶的,因为一年多来没有编译器可以实际构建上述代码:-)
@Mikhail:clang 和 g++ 现在都应该支持继承构造函数:clang.llvm.org/cxx_status.html gcc.gnu.org/projects/cxx0x.html 推荐将此作为正确答案。
我是来自未来的人! MSVC 2015 声明支持。
您可以使用此语法仅继承特定的构造函数吗?
@whoKnows:不,这是全有或全无:en.cppreference.com/w/cpp/language/using_declaration
n
nijoakim

构造函数不被继承。它们被子构造函数隐式或显式调用。

编译器创建一个默认构造函数(一个不带参数)和一个默认复制构造函数(一个带有引用相同类型的参数)。但是,如果您想要一个接受 int 的构造函数,则必须明确定义它。

class A
{
public: 
    explicit A(int x) {}
};

class B: public A
{
public:
    explicit B(int x) : A(x) { }
};

更新:在 C++11 中,可以继承构造函数。有关详细信息,请参阅 Suma 的答案。


J
Jean-François Fabre

这直接来自 Bjarne Stroustrup's page

如果你这样选择,你仍然可以通过继承派生类中的构造函数来打自己的脚,在派生类中定义需要初始化的新成员变量:

struct B1 {
    B1(int) { }
};

struct D1 : B1 {
    using B1::B1; // implicitly declares D1(int)
    int x;
};

void test()
{
    D1 d(6);    // Oops: d.x is not initialized
    D1 e;       // error: D1 has no default constructor
}

请注意,使用另一个很棒的 C++11 功能(成员初始化):

 int x = 77;

代替

int x;

会解决问题


E
Evan Teran

您必须在 B 中显式定义构造函数并显式调用父级的构造函数。

B(int x) : A(x) { }

或者

B() : A(5) { }

P
Pradu

使用模板函数绑定所有构造函数怎么样?

template <class... T> Derived(T... t) : Base(t...) {}

可能你应该用完美的转发来做到这一点: template < typename ... Args > B( Args && ... args ) : A( std::forward< Args >( args ) ... ) {}
而您刚刚破坏了 Derived 的复制构造函数。
Base 的构造函数也必须模板化吗?当您调用 Base(t...) 时,无论 t 是什么,都必须对 Base 进行模板化?
I
Iqbal Haider

正确的代码是

class A
{
    public: 
      explicit A(int x) {}
};

class B: public A
{
      public:

     B(int a):A(a){
          }
};

main()
{
    B *b = new B(5);
     delete b;
}

错误是 b/c 类 B 没有参数构造函数,其次它应该有基类初始化程序来调用基类参数构造函数的构造函数


E
Eugen Bondarev

以下是我如何让派生类“继承”所有父类的构造函数。我发现这是最直接的方法,因为它只是将所有参数传递给父类的构造函数。

class Derived : public Parent {
public:
  template <typename... Args>
  Derived(Args&&... args) : Parent(std::forward<Args>(args)...) 
  {

  }
};

或者,如果您想要一个不错的宏:

#define PARENT_CONSTRUCTOR(DERIVED, PARENT)                    \
template<typename... Args>                                     \
DERIVED(Args&&... args) : PARENT(std::forward<Args>(args)...)

class Derived : public Parent
{
public:
  PARENT_CONSTRUCTOR(Derived, Parent)
  {
  }
};

不要做第一件事,地狱不要做宏。第一部分定义复制和移动构造函数。
S
Sarath Govind

派生类继承了基类的所有成员(字段和方法),但派生类不能继承基类的构造函数,因为构造函数不是类的成员。不让派生类继承构造函数,只允许调用基类的构造函数

class A
{
    public: 
        explicit A(int x) {}
};

class B: public A
{
       B(int x):A(x);
};

int main(void)
{
    B *b = new B(5);
    delete b;
}