为什么编译器提供默认拷贝构造函数 [英] Why compiler provides default copy constructor
问题描述
我想知道为什么编译器提供了默认的拷贝构造函数.Whats是这个想法背后的策略。
I wanted to know Why compiler provides default copy constructor..Whats the strategy behind that idea.
感谢在前进。
推荐答案
从相关(但不完全相同)的问题 - http://stackoverflow.com/questions/217911/why-dont-c-compilers-define-operator-and-operator/218713#218713 :
From a related (but not same) question - http://stackoverflow.com/questions/217911/why-dont-c-compilers-define-operator-and-operator/218713#218713:
Stroustrup对C ++的设计与演进(第11.4.1节 - 复制控制)中的默认复制构造函数说:
Stroustrup said this about the default copy constructor in "The Design and Evolution of C++" (Section 11.4.1 - Control of Copying):
我个人认为不幸的是,复制操作是默认定义的,我禁止复制我的许多类的对象。但是,C ++从C继承了它的默认赋值和拷贝构造函数,并且经常使用它们。
I personally consider it unfortunate that copy operations are defined by default and I prohibit copying of objects of many of my classes. However, C++ inherited its default assignment and copy constructors from C, and they are frequently used.
所以答案是Stroustrup不愿意向后兼容C(可能是C ++大多数疣的原因,但也可能是C ++流行的主要原因)。
So the answer is that it was included reluctantly by Stroustrup for backwards compatibility with C (probably the cause of most of C++'s warts, but also probably the primary reason for C++'s popularity).
为了我自己的目的,我的IDE我使用的新类的片段包含一个私人赋值运算符和复制构造函数的声明,所以当我创建一个新类,我没有得到默认分配和复制操作 - 我必须显式地删除这些操作的声明从私人:section如果我想让编译器能够为我生成它们。
For my own purposes, in my IDE the snippet I use for new classes contains declarations for a private assignment operator and copy constructor so that when I gen up a new class I get no default assignment and copy operations - I have to explicitly remove the declaration of those operations from the private: section if I want the compiler to be able to generate them for me.
这篇关于为什么编译器提供默认拷贝构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!