Default copy constructor & its problems

Possible Duplicate:
What is The Rule of Three?

Why is it recommended to provide implementation of copy constructor instead of using compiler provided "default copy constructor" ?


如果你的类包含指针成员, 它们是动态分配的,那么你需要提供你自己的拷贝构造版本,因为默认版本只是对它们进行浅度拷贝。


It's not.

The default copy constructor is perfect in 99.9% of cases.

The exception of classes with owned pointers. Here the shallow copy of the default copy constructor does not work as expected for beginners.

But then you should never have pointers in your class so it becomes a non issue. To make this clear any owned pointers should be contained in a smart pointer (or container type) object. So it is a non issue.

If you are writing a smart pointer or container like object, then you need to implement the rule of three.

链接地址: http://www.djcxy.com/p/73188.html

上一篇: 根据价值确定范围并传递课程?

下一篇: 默认拷贝构造函数及其问题