C++ 复制构造函数和默认构造函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1585708/
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
Copy Constructor and default constructor
提问by Light_handle
Do we have to explicitly define a default constructor when we define a copy constructor for a class?? Please give reasons.
为类定义复制构造函数时,是否必须显式定义默认构造函数?请给出理由。
eg:
例如:
class A
{
int i;
public:
A(A& a)
{
i = a.i; //Ok this is corrected....
}
A() { } //Is this required if we write the above copy constructor??
};
Also, if we define any other parameterized constructor for a class other than the copy constructor, do we also have to define the default constructor?? Consider the above code without the copy constructor and replace it with
另外,如果我们为除复制构造函数之外的类定义任何其他参数化构造函数,我们是否还必须定义默认构造函数??考虑上面没有复制构造函数的代码并将其替换为
A(int z)
{
z.i = 10;
}
Alrite....After seeing the answers I wrote the following program.
Alrite....看到答案后,我编写了以下程序。
#include <iostream>
using namespace std;
class X
{
int i;
public:
//X();
X(int ii);
void print();
};
//X::X() { }
X::X(int ii)
{
i = ii;
}
void X::print()
{
cout<<"i = "<<i<<endl;
}
int main(void)
{
X x(10);
//X x1;
x.print();
//x1.print();
}
ANd this program seems to be working fine without the default constructor. Please explain why is this the case?? I am really confused with the concept.....
并且这个程序似乎在没有默认构造函数的情况下运行良好。请解释为什么会这样??我真的对这个概念感到困惑......
回答by AnT
Yes. Once you explicitly declare absolutely anyconstructor for a class, the compiler stops providing the implicit default constructor. If you still need the default constructor, you have to explicitly declare and define it yourself.
是的。一旦您为类显式声明了任何构造函数,编译器将停止提供隐式默认构造函数。如果您仍然需要默认构造函数,则必须自己显式声明和定义它。
P.S. It is possible to write a copy constructor (or conversion constructor, or any other constructor) that is also defaultconstructor. If your new constructor falls into that category, there's no need to provide an additional default constructor anymore :)
PS 可以编写一个也是默认构造函数的复制构造函数(或转换构造函数,或任何其他构造函数)。如果您的新构造函数属于该类别,则无需再提供额外的默认构造函数 :)
For example:
例如:
// Just a sketch of one possible technique
struct S {
S(const S&);
S(int) {}
};
S dummy(0);
S::S(const S& = dummy) {
}
In the above example the copy constructor is at the same time the default constructor.
在上面的例子中,复制构造函数同时也是默认构造函数。
回答by Steve314
You don't have to define both. However, once you define any constructors for a class, all the default ones become unavailable. So - if you want to both copy-construct and to construct without copying, you need to define an non-default (ie explicit) default (ie no parameters) constructor too.
您不必定义两者。但是,一旦为类定义了任何构造函数,所有默认构造函数都将不可用。所以 - 如果你想要复制构造和不复制构造,你也需要定义一个非默认(即显式)默认(即无参数)构造函数。
If you define a copy constructor, you should normally override the assignment operator too.
如果你定义了一个复制构造函数,你通常也应该覆盖赋值运算符。
回答by coppro
As AndreyT said, if you explicitly declare any constructor, including a copy constructor, the compiler will not implicitly declare or define a default constructor.
正如 AndreyT 所说,如果您显式声明任何构造函数,包括复制构造函数,编译器将不会隐式声明或定义默认构造函数。
This is not always a problem.
这并不总是一个问题。
If you do not want your class to be default-constructible, then it's perfectly fine not to declare a default constructor. But if you want it to be default-constructible (for instance, if you want to uncomment the X x1;
line in your example), then you must declare and define a default constructor.
如果您不希望您的类是可默认构造的,那么不声明默认构造函数是完全可以的。但是,如果您希望它是默认构造函数(例如,如果您想取消对X x1;
示例中的行的注释),那么您必须声明并定义一个默认构造函数。
Also note that a default constructor is any constructor that can be called with no arguments, not just one with no arguments. X::X(int = 5)
is a perfectly fine default constructor.
另请注意,默认构造函数是可以不带参数调用的任何构造函数,而不仅仅是不带参数的构造函数。X::X(int = 5)
是一个完美的默认构造函数。
回答by Whoami
1) Constructor with no argument is called 'default Constructor'. 2) If user have not provided any of the following constructor, then the compiler declares the default constructor for you.
1) 没有参数的构造函数称为“默认构造函数”。2) 如果用户未提供以下任何构造函数,则编译器会为您声明默认构造函数。
2a) Copy Constructor
2b) Non-default constructor
2C) default constructor.
3) If compilers declares the 'default constructor', then it is said to be 'implicitly declared default constructor',
3) 如果编译器声明了“默认构造函数”,则称其为“隐式声明的默认构造函数”,
4) The 'implicitly declared default constructor' are of two types
4) '隐式声明的默认构造函数'有两种类型
4a) **trivial** Constructor
4b) **non-trivial** Constructor.
5) Implictly declared default constructor is said to be 'trivial', when there is 5a) No reference variable 5b) No Virtual Function 5c) No Virtual Base Class. 5d) Parent class has 'trivial' constructor,
5) 当有 5a) 没有引用变量 5b) 没有虚拟函数 5c) 没有虚拟基类时,隐式声明的默认构造函数被认为是“平凡的”。5d) 父类有“平凡”的构造函数,
Else, it is said to be 'non-trivial'.
Hope this helps.
希望这可以帮助。