Questions tagged [auto-ptr]

A C++ template class that provides a limited garbage collection facility for pointers, by allowing pointers to have the elements they point to automatically destroyed when the auto_ptr object is itself destroyed. Deprecated as of C++11 in favor of unique_ptr.

auto_ptr is deprecated as of C++11. For the C++11 equivalent, unique_ptr, see the reference page.

Reference page for auto_ptr.

197 questions
12
votes
2 answers

Is it true that a unique_ptr declaration, unlike a auto_ptr declaration, is well-defined when its template type is of an incomplete type?

I wrote this article and got some comments on it that confused me. It basically boils down to my having seen T2 used only as a template parameter and mistakenly jumped to the conclusion that I could therefore take the opportunity of forward…
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
11
votes
3 answers

what is auto_ptr_ref, what it achieves and how it achieves it

auto_ptr_ref documentation here says this This is an instrumental class to allow certain conversions that allow auto_ptr objects to be passed to and returned from functions. Can somebody explain how auto_ptr_ref helps in achieving this. I just…
Yogesh Arora
  • 2,216
  • 2
  • 25
  • 35
10
votes
5 answers

how to test whether auto pointer is null?

I'm new to auto pointer. I have this: std::auto_ptr myPointer(new MyClass(someArg)); How do I test whether I can instantiate myPointer successfully? I tried if (myPointer==NULL) and the compiler emitted an error: no operator "==" matches…
user853069
  • 491
  • 2
  • 6
  • 12
10
votes
4 answers

auto_ptr or shared_ptr equivalent in managed C++/CLI classes

In C++/CLI , you can use native types in a managed class by it is not allowed to hold a member of a native class in a managed class : you need to use pointers in that case. Here is an example : class NativeClass { .... }; public ref class…
Pascal T.
  • 3,866
  • 4
  • 33
  • 36
9
votes
4 answers

Why doesn't auto_ptr construction work using = syntax

I ran into a compiler error that didn't make much sense to me: #include using namespace std; auto_ptr table = db->query("select * from t"); error: conversion from 'Table*' to non-scalar type 'std::auto_ptr< Table>'…
Frank Krueger
  • 69,552
  • 46
  • 163
  • 208
8
votes
1 answer

Is there a contradiction between these two sources about the `auto_ptr` template class?

This site states on "Ownership, Sources, and Sinks" : "When you copy an auto_ptr, you automatically transfer ownership from the source auto_ptr to the target auto_ptr; if the target auto_ptr already owns an object, that object is first freed. After…
Belloc
  • 6,318
  • 3
  • 22
  • 52
8
votes
3 answers

What is the difference between auto pointers and shared pointers in C++

I have heard that auto pointers own their object whereas shared pointers can have many objects pointing to them. Why dont we use shared pointers all the time. In relation to this what are smart pointers, people use this term interchangeably with…
shreyasva
  • 13,126
  • 25
  • 78
  • 101
8
votes
9 answers

How do use a std::auto_ptr in a class you have to copy construct?

I have class foo that contains a std::auto_ptr member that I would like to copy construct but this does not appear to be allowed. There's a similar thing for the assignment. See the following example: struct foo { private: int _a; …
Pascal Dennerly
  • 212
  • 4
  • 9
7
votes
1 answer

What's the best way to return something like a collection of `std::auto_ptr`s in C++03?

std::auto_ptr is not allowed to be stored in an STL container, such as std::vector. However, occasionally there are cases where I need to return a collection of polymorphic objects, and therefore I can't return a vector of objects (due to the…
Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
7
votes
4 answers

What is the difference between *ptr and *ptr.get() when using auto_ptr?

Why would I use get() with *, instead of just calling *? Consider the following code: auto_ptr p (new int); *p = 100; cout << "p points to " << *p << '\n'; //100 auto_ptr p (new int); *p.get() = 100; cout << "p points to " <<…
user2856064
  • 541
  • 1
  • 8
  • 25
7
votes
2 answers

Compilation problems with vector >

Consider the following code: #include #include #include using namespace std; struct A { int a; A(int a_):a(a_) {} }; int main() { vector > as; for (int i = 0; i < 10; i++) { …
petersohn
  • 11,292
  • 13
  • 61
  • 98
7
votes
1 answer

Can i pass auto_ptr by reference to functions?

is the following function OK: void DoSomething(auto_ptr< … >& a)....
amitlicht
  • 2,868
  • 6
  • 23
  • 27
7
votes
2 answers

std::auto_ptr, delete[] and leaks

Why this code does not cause memory leaks? int iterCount = 1000; int sizeBig = 100000; for (int i = 0; i < iterCount; i++) { std::auto_ptr buffer(new char[sizeBig]); } WinXP sp2, Compiler : BCB.05.03
Mikhail Aksenov
  • 944
  • 9
  • 23
7
votes
1 answer

Why can't I have an auto_ptr in an Exception class

I have a problem with auto_ptr in Exception classes, that I eventually reduced to: #include class MyException { std::auto_ptr m_foo2; }; int main() { try { throw MyException(); } catch (const…
Douglas Leeder
  • 52,368
  • 9
  • 94
  • 137
6
votes
4 answers

Why vector.push_back(auto_ptr) wouldn't compile?

I learned that STL can forbid programmer putting an auto_ptr into a container. For example following code wouldn't compile: auto_ptr a(new int(10)); vector > v; v.push_back(a); auto_ptr has the copy constructor, why…
frinker
  • 63
  • 6
1
2
3
13 14