copy constructor 的使用时机【源自c++ primer】

来源:互联网 发布:js获取字符串位置 编辑:程序博客网 时间:2024/06/05 10:11

 

For many classes, the synthesized copy constructor does exactly the work that is needed.Classes that contain only members that are of class type or members that are of built-in (butnot pointer type) often can be copied without explicitly defining the copy constructor.

However, some classes must take control of what happens when objects are copied. Such classes often have a data member that is a pointer or that represents another resource that is allocated in the constructor. Other classes have bookkeeping that must be done whenever a new object is created. In both these cases, the copy constructor must be defined.

防止copy的办法

To prevent copies, a class must explicitly declare its copy constructor as private .

 

原创粉丝点击