C++ 继承构造函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/347358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Inheriting constructors
提问by Sydius
Why does this code:
为什么这段代码:
class A
{
public:
explicit A(int x) {}
};
class B: public A
{
};
int main(void)
{
B *b = new B(5);
delete b;
}
Result in these errors:
导致这些错误:
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&)
Shouldn't B inherit A's constructor?
B 不应该继承 A 的构造函数吗?
(this is using gcc)
(这是使用gcc)
回答by Suma
If your compiler supports C++11 standard, there is a constructor inheritance using using
(pun intended). For more see Wikipedia C++11 article. You write:
如果您的编译器支持 C++11 标准,则使用using
(双关语)构造函数继承。有关更多信息,请参阅维基百科 C++11 文章。你写:
class A
{
public:
explicit A(int x) {}
};
class B: public A
{
using A::A;
};
This is all or nothing - you cannot inherit only some constructors, if you write this, you inherit all of them. To inherit only selected ones you need to write the individual constructors manually and call the base constructor as needed from them.
这要么全有要么全无——你不能只继承一些构造函数,如果你写这个,你就继承了所有的构造函数。要仅继承选定的构造函数,您需要手动编写各个构造函数并根据需要从它们调用基本构造函数。
Historically constructors could not be inherited in the C++03 standard. You needed to inherit them manually one by one by calling base implementation on your own.
从历史上看,构造函数不能在 C++03 标准中继承。您需要通过自己调用基本实现来手动一一继承它们。
回答by Avi
Constructors are not inherited. They are called implicitly or explicitly by the child constructor.
构造函数不是继承的。它们由子构造函数隐式或显式调用。
The compiler creates a default constructor (one with no arguments) and a default copy constructor (one with an argument which is a reference to the same type). But if you want a constructor that will accept an int, you have to define it explicitly.
编译器创建一个默认构造函数(一个没有参数)和一个默认复制构造函数(一个参数是对同一类型的引用)。但是,如果您想要一个接受 int 的构造函数,则必须明确定义它。
class A
{
public:
explicit A(int x) {}
};
class B: public A
{
public:
explicit B(int x) : A(x) { }
};
UPDATE: In C++11, constructors can be inherited. See Suma's answer for details.
更新:在 C++11 中,构造函数可以被继承。有关详细信息,请参阅 Suma 的回答。
回答by grepsedawk
You have to explicitly define the constructor in B and explicitly call the constructor for the parent.
您必须在 B 中显式定义构造函数并为父显式调用构造函数。
B(int x) : A(x) { }
or
或者
B() : A(5) { }
回答by Pradu
How about using a template function to bind all constructors?
使用模板函数绑定所有构造函数怎么样?
template <class... T> Derived(T... t) : Base(t...) {}
回答by nenchev
This is straight from Bjarne Stroustrup's page:
这直接来自Bjarne Stroustrup 的页面:
If you so choose, you can still shoot yourself in the foot by inheriting constructors in a derived class in which you define new member variables needing initialization:
如果您选择这样做,您仍然可以通过在派生类中继承构造函数,在该派生类中定义需要初始化的新成员变量:
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
}
回答by Iqbal Haider
Correct Code is
正确的代码是
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;
}
Error is b/c Class B has not parameter constructor and second it should have base class initializer to call the constructor of Base Class parameter constructor
错误是 b/c 类 B 没有参数构造函数,其次它应该有基类初始值设定项来调用基类参数构造函数的构造函数