Tuesday, October 12, 2010

Class with an auto_ptr as a Member

If you have a class with an auto_ptr as a member, you must define or declare your own constructor, and copy constructor for your class.

This is needed to ensure a copy does what you want, otherwise when you make a copy of an object of this type, the destination auto_ptr member will take ownership from the source object. The compiler won't complain, but most likely your class won't do what you expect. If you define a copy constructor, the compiler will not provide a default construct. You will have to provide your own.


Reference: More Exceptional C++ by Herb Sutter. Addison-Wesley, 2002., pp. 148, 185.

No comments:

Post a Comment