为什么编译器提供了默认的拷贝构造函数 [英] Why compiler provides default copy constructor

查看:121
本文介绍了为什么编译器提供了默认的拷贝构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么编译器提供默认的拷贝构造函数.Whats是这个想法背后的策略。

I wanted to know Why compiler provides default copy constructor..Whats the strategy behind that idea.

感谢在前进。

推荐答案

从相关(但不完全相同)的问题 - 为什么C ++编译器不定义operator ==和operator!=?

From a related (but not same) question - Why don't C++ compilers define operator== and operator!=?:

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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆