Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ std::auto_ptr copy constructor

Tags:

c++

auto-ptr

std::auto_ptr lacks const copy constructor, therefore I cannot use it directly in collections. is there some way to have for example vector of std::auto_ptr without using boost pointer collection template?

like image 387
Anycorn Avatar asked Mar 04 '26 07:03

Anycorn


2 Answers

If you have a C++0x compiler you can use shared_ptr or unique_ptr as appropriate.

There is a good example of correct unique_ptr usage here courtesy of @James McNellis. For a shared_ptr walkthrough look here, courtesy of @D.Shawley. [Upvotes would still be appreciated on those threads, I am sure.]

vector of auto_ptr is always invalid, although Visual C++ v6 disagreed.

like image 200
Steve Townsend Avatar answered Mar 05 '26 19:03

Steve Townsend


No, you just can't have a vector of std::auto_ptr, though there exist many speculations that you can. But if your compiler supports C++0x, you can use std::unique_ptr, which is the new alternative of the deprecated auto pointer which, quote from the new standard, provides a superior alternative. See also this thread

like image 43
Armen Tsirunyan Avatar answered Mar 05 '26 21:03

Armen Tsirunyan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!