C++ 我们什么时候需要有一个默认构造函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5498937/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:17:09  来源:igfitidea点击:

When do we need to have a default constructor?

c++language-lawyer

提问by XMarshall

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() {}
};
  1. Why is the compiler not generating the zero argument default constructor implicitly in the class Rect?

  2. 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.

  1. 为什么编译器不在类 Rect 中隐式生成零参数默认构造函数?

  2. 据我所知,如果一个类(Rect)派生自另一个具有默认构造函数(隐式生成或显式提供)的类(Shape),则默认构造函数应该由编译器生成。

采纳答案by Lightness Races in Orbit

A default constructor is not synthesised if you created your own constructor with arguments. Since you gave Shapea constructor of your own, you'd have to explicitly write out a default Shapeconstructor now:

如果您使用参数创建了自己的构造函数,则不会合成默认构造函数。由于您提供Shape了自己的构造函数,因此您现在必须明确写出默认Shape构造函数:

class Shape
{
      int k;

  public:
      Shape() : k(0) {}
      Shape(int n) : k(n) {}
      ~Shape() {}
};

(You can leave out the empty ~Rect() {}definitions, as these will be synthesised.)

(您可以省略空~Rect() {}定义,因为这些将被综合。)

However, it looks to me like you don't wanta default constructor for Shape here. Have Rectconstruct the Shapebase properly:

然而,它看起来像你对我不想要的形状的默认构造函数在这里。已正确Rect构建Shape基础:

class Shape
{
      int area; // I've had to guess at what this member means. What is "k"?!

  public:
      Shape(const int area)
         : area(area)
      {}
};

class Rect : public Shape
{
     int l;
     int w;

  public:
     Rect(const int l, const int w)
        : Shape(l*w)
        , l(l)
        , w(w)
     {}
};

Also note that this example is oft cited as an abuse of OO. Consider whether you reallyneed inheritance here.

另请注意,此示例经常被引用为滥用 OO。考虑一下这里是否真的需要继承。

回答by Nick

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:

而且您还需要通过调用来构建您的基类:

Rect( int n ) : Shape( n ), l(n)
{
}

回答by Jerry Coffin

The compiler will define a default ctor if and only if you don't explicitly declare any ctors.

当且仅当您没有显式声明任何 ctor 时,编译器才会定义默认 ctor。

Note that what's important is declaringthe 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.

请注意,重要的是声明构造函数,而不是定义它。例如,声明一个私有 ctor 而从不定义它是相当常见的,以防止编译器隐式定义任何其他 ctor。

Edit: Also note that C++11 has an =defaultsyntax for dealing with situations like yours.

编辑:另请注意,C++11 具有=default处理类似情况的语法。

回答by Mike Thomsen

See this for the full behaviors of C++ WRT constructors: http://en.wikipedia.org/wiki/Default_constructor

有关 C++ WRT 构造函数的完整行为,请参阅此内容:http: //en.wikipedia.org/wiki/Default_constructor

The simple answer is that if you specify a constructor, the compiler will notcreate a default one for you.

简单的答案是,如果您指定构造函数,编译器将不会为您创建默认构造函数。

This rule applies to Java as well.

此规则也适用于 Java。

回答by Bo Persson

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.

据说,如果您需要在类中进行一些特殊的初始化,默认构造函数将不会做正确的事情。

回答by das_weezul

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.

当您为 Shape 定义一个需要整数的构造函数时,您已经通过这样做覆盖了默认构造函数。因此,如果您扩展 Shape,则必须将整数值传递给超类。

回答by Sarfraz Ahmed

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.

如果您没有定义任何构造函数,编译器会生成默认构造函数。但是,如果您定义了任何带参数或不带参数的构造函数。编译器将使用该构造函数,并且不会生成具有零参数的默认构造函数。