什么时候我们需要有一个默认构造函数?
我的问题很简单。什么时候我们需要有一个默认构造函数? 请参考下面的代码:
class Shape
{
int k;
public:
Shape(int n) : k(n) {}
~Shape() {}
};
class Rect : public Shape
{
int l;
public:
Rect(int n): l(n)
{} //error C2512: 'Shape' : no appropriate default constructor available
~Rect() {}
};
为什么编译器没有在类 Rect 中隐式生成零参数默认构造函数?
据我所知,如果一个类(Rect)派生自另一个具有默认构造函数(隐式生成或显式提供)的类(Shape),则默认构造函数应该由编译器生成。
My question is simple. When do we need to have a default constructor?
Please refer to the code below:
class Shape
{
int k;
public:
Shape(int n) : k(n) {}
~Shape() {}
};
class Rect : public Shape
{
int l;
public:
Rect(int n): l(n)
{} //error C2512: 'Shape' : no appropriate default constructor available
~Rect() {}
};
Why is the compiler not generating the zero argument default constructor implicitly in the class Rect?
As per my knowledge, if a class (Rect) is derived from another class (Shape) that has default constructor (either implicitly generated or explicitly provided), the default constructor should be generated by the compiler.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果您使用参数创建了自己的构造函数,则不会合成默认构造函数。由于您为
Shape
提供了自己的构造函数,因此您现在必须显式写出默认的Shape
构造函数:(您可以省略空的
~Rect () {}
定义,因为这些将被综合。)但是,在我看来,您不想要这里的 Shape 的默认构造函数。让
Rect
正确构建Shape
基础:另请注意,此示例是 经常被认为是对面向对象的滥用。考虑一下您是否真的需要继承。
A default constructor is not synthesised if you created your own constructor with arguments. Since you gave
Shape
a constructor of your own, you'd have to explicitly write out a defaultShape
constructor now:(You can leave out the empty
~Rect() {}
definitions, as these will be synthesised.)However, it looks to me like you don't want a default constructor for Shape here. Have
Rect
construct theShape
base properly:Also note that this example is oft cited as an abuse of OO. Consider whether you really need inheritance here.
如果没有定义其他构造函数,编译器只会自动生成默认构造函数。不管有什么继承。
您还需要通过调用来构造基类:
A default constructor will only be automatically generated by the compiler if no other constructors are defined. Regardless of any inheritance.
And also you need to construct your base class by calling:
当且仅当您未显式声明任何构造函数时,编译器才会定义默认构造函数。
请注意,重要的是声明构造函数,而不一定是定义它。例如,声明一个私有构造函数,但从不定义它,以防止编译器隐式定义任何其他构造函数,这是相当常见的。
编辑:另请注意,C++11 有一个
=default
语法来处理像您这样的情况。The compiler will define a default ctor if and only if you don't explicitly declare any ctors.
Note that what's important is declaring the constructor, not necessarily defining it. It's fairly common, for example, to declare a private ctor, and never define it, to prevent the compiler from implicitly defining any others.
Edit: Also note that C++11 has an
=default
syntax for dealing with situations like yours.有关 C++ WRT 构造函数的完整行为,请参阅此内容: http://en.wikipedia.org/wiki/Default_constructor< /a>
简单的答案是,如果您指定构造函数,编译器将不会为您创建默认构造函数。
该规则也适用于 Java。
See this for the full behaviors of C++ WRT constructors: http://en.wikipedia.org/wiki/Default_constructor
The simple answer is that if you specify a constructor, the compiler will not create a default one for you.
This rule applies to Java as well.
如果您没有定义任何构造函数,编译器会生成默认构造函数。但是,如果您定义了任何带有或不带参数的构造函数。编译器将使用该构造函数,并且不会生成带有零参数的默认构造函数。
Compiler generates default constructor in case when you have not define any constructor. But if you have defined any constructor that takes some argument or not. The compiler will use that constructor and will not generate default constructor with zero argument.
仅当您未定义任何其他构造函数时才会生成默认构造函数。
据说,如果您需要在类中进行一些特殊的初始化,默认构造函数将不会做正确的事情。
The default constructor is generated only if you have not defined any other constructors.
Supposedly, if you need some special initialization in the class, the default constructor would not do the right thing.
当您为 Shape 定义一个构造函数并期望一个整数时,您已经通过这样做覆盖了默认构造函数。因此,如果扩展 Shape,则必须将整数值传递给超类。
As you defined a Constructor for Shape expecting an integer you have overwritten the default constructor by doing so. So if you extend Shape you must pass an integer value to the superclass.