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?
为什么编译器没有在类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.
据我所知,如果一个类(Rect)派生自另一个具有默认构造函数的类(Shape)(隐式生成或显式提供),则默认构造函数应由编译器生成。
7 个解决方案
#1
19
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 default Shape
constructor 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 want a default constructor for Shape here. Have Rect
construct the Shape
base properly:
但是,在我看来,你不想在这里使用Shape的默认构造函数。让Rect正确构造Shape基础:
class Shape
{
int area; // I've had to guess at what this member means. What is "k"?!
public:
Shape(int n) : area(area) {}
};
class Rect : public Shape
{
int l;
int w;
public:
Rect(int l, 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 really need inheritance here.
另请注意,此示例被引用为滥用OO。考虑一下你是否真的需要继承。
#2
11
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)
{
}
#3
2
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 not create a default one for you.
简单的答案是,如果指定构造函数,编译器将不会为您创建默认构造函数。
This rule applies to Java as well.
此规则也适用于Java。
#4
2
The compiler will define a default ctor if and only if you don't explicitly declare any ctors.
当且仅当您没有明确声明任何ctors时,编译器才会定义默认ctor。
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.
请注意,重要的是声明构造函数,而不是定义构造函数。例如,声明私有ctor并且从不定义它是相当常见的,以防止编译器隐式定义任何其他的。
Edit: Also note that C++11 has an =default
syntax for dealing with situations like yours.
编辑:还要注意C ++ 11有一个= default语法来处理像你这样的情况。
#5
0
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.
据说,如果你需要在类中进行一些特殊的初始化,默认的构造函数就不会做正确的事情。
#6
0
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,则必须将整数值传递给超类。
#7
0
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.
如果您没有定义任何构造函数,编译器会生成默认构造函数。但是如果你已经定义了任何带有参数的构造函数。编译器将使用该构造函数,并且不会生成带零参数的默认构造函数。
#1
19
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 default Shape
constructor 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 want a default constructor for Shape here. Have Rect
construct the Shape
base properly:
但是,在我看来,你不想在这里使用Shape的默认构造函数。让Rect正确构造Shape基础:
class Shape
{
int area; // I've had to guess at what this member means. What is "k"?!
public:
Shape(int n) : area(area) {}
};
class Rect : public Shape
{
int l;
int w;
public:
Rect(int l, 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 really need inheritance here.
另请注意,此示例被引用为滥用OO。考虑一下你是否真的需要继承。
#2
11
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)
{
}
#3
2
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 not create a default one for you.
简单的答案是,如果指定构造函数,编译器将不会为您创建默认构造函数。
This rule applies to Java as well.
此规则也适用于Java。
#4
2
The compiler will define a default ctor if and only if you don't explicitly declare any ctors.
当且仅当您没有明确声明任何ctors时,编译器才会定义默认ctor。
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.
请注意,重要的是声明构造函数,而不是定义构造函数。例如,声明私有ctor并且从不定义它是相当常见的,以防止编译器隐式定义任何其他的。
Edit: Also note that C++11 has an =default
syntax for dealing with situations like yours.
编辑:还要注意C ++ 11有一个= default语法来处理像你这样的情况。
#5
0
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.
据说,如果你需要在类中进行一些特殊的初始化,默认的构造函数就不会做正确的事情。
#6
0
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,则必须将整数值传递给超类。
#7
0
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.
如果您没有定义任何构造函数,编译器会生成默认构造函数。但是如果你已经定义了任何带有参数的构造函数。编译器将使用该构造函数,并且不会生成带零参数的默认构造函数。