Why compiler provides default copy constructor
I wanted to know Why compiler provides default copy constructor..Whats the strategy behind that idea.
Thanks in Advance.
From a related (but not same) question - Why don't C++ compilers define operator== and operator!=?:
Stroustrup said this about the default copy constructor in "The Design and Evolution of C++" (Section 11.4.1 - Control of Copying):
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.
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).
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.
From The C++ Programming Language, Section 11.3.4 Copying
...for types where the default copy constructor has the right semantics, I prefer to rely on that default. It is less verbose than anything I can write, and people should understand the default. Also, compilers know about the default and its possible optimization opportunities. Furthermore, writing out the memberwise copy by hand is tedious and error-prone for classes with many data members.
Basically, I read that as the default copy constructor saves you the effort, saves you from making errors caused by tedium, and helps optimize your code by removing the the temptation to optimize it by hand (by letting the compiler do it).
否则,当你通过值传递实例时,编译器如何生成一个实例?
链接地址: http://www.djcxy.com/p/73766.html上一篇: 这是一个安全的方式来实现一个通用的运算符==和运算符<?
下一篇: 为什么编译器提供默认拷贝构造函数