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
1
vote
2 answers

Cast auto_ptr to auto_ptr

Please help me to understand the following issue. Look at the code example below: #include class Shape { public: virtual wchar_t *GetName() { return L"Shape"; } }; class Circle: public Shape { public: wchar_t *GetName() { return…
nickolay
  • 3,643
  • 3
  • 32
  • 40
1
vote
2 answers

References and auto_ptr

If I have a auto_ptr I can pass it for a reference?Like: auto_ptrClass(new MyClass); void SetOponent(MyClass& oponent); //So I pass SetOponent(Class) And what is odd copy behavior of auto_ptrs?
Hai
  • 4,764
  • 8
  • 29
  • 26
1
vote
1 answer

Type of a global object dependent on boolean flag (determined at runtime)

I want to do something like the following if (flag) { type1_t object = ..... } else { type2_t object = ..... } // do the same thing with object type1_t and type2_t are custom classes. The problem with the above snippet is object remains local…
roulette01
  • 1,984
  • 2
  • 13
  • 26
1
vote
3 answers

Difference between ATL CAutoPtr and STL std::auto_ptr?

I'm writing some code in MFC and I want to use auto pointers. I've come across two different classes that look like they do the same thing: CAutoPtr and std::auto_ptr What are people's thoughts about the two different implementations? Further, I…
Jared
  • 5,977
  • 10
  • 38
  • 48
1
vote
2 answers

Re-assinging an "auto_ptr" and Managing Memory

I've a situation like this: class MyClass { private: std::auto_ptr obj; public: MyClass() { obj = auto_ptr(new MyOtherClass()); } void reassignMyOtherClass() { // ... do funny stuff MyOtherClass…
Srikanth
  • 11,780
  • 23
  • 72
  • 92
1
vote
3 answers

Why unique_ptr works but auto_ptr doesn’t with STL

I have referred to lot of StackOverflow links on these questions where the reason for auto_ptr not working well with STL is std::auto_ptr<> does not fulfill the requirements of being copy-constructible and assignable (since auto_ptr has a fake copy…
anurag86
  • 1,635
  • 1
  • 16
  • 31
1
vote
2 answers

Why is using a reference or unique pointer member of a class a bad thing?

In the book "C++ Coding Standards. 101 Rules, Guidelines, and Best Practices" by Herb Sutter and Andrei Alexandrescu in Rule 52, the final quote is: "In rare cases, classes that have members of strange types (e.g., references, std::auto_ptrs)…
Roman2452809
  • 378
  • 1
  • 2
  • 17
1
vote
1 answer

Resurrecting std::auto_ptr in GCC when compiling with -std=c++17

Is there a macro or compiler flag that would let me keep using auto_ptr in GCC 7/8 with -std=c++17? I have easily found the corresponding macro for clang and MSVC, but my Google-fu is not good enough to find the solution for GCC. I don't want to use…
Violet Giraffe
  • 32,368
  • 48
  • 194
  • 335
1
vote
5 answers

how to reset a large number of class members at once, not in a destructor?

I have a class containing many members of a simple class type. More importantly, their number is growing as I go on with the development. I need to be able to reset them all at once, and I'd like to do it without copy-pasting them. The code…
davka
  • 13,974
  • 11
  • 61
  • 86
1
vote
1 answer

How to debug regasm (what types get registered)

We have a Managed C++ DLL that when registered with regasm appears to add some junk types to the registry. Within the class Blah, any private variables utilizing MyTeam.ManagedAutoPtr get added to the registry. My question at this point is whether…
Mijin
  • 125
  • 1
  • 9
1
vote
1 answer

Misleading performance warning for auto_ptr type passed by value

I'm checking one of my projects with Cppcheck 1.75, and for this code (reduced for clarity): class TJob { public: TJob(std::auto_ptr task); // ... private: std::auto_ptr m_task; }; TJob::TJob( std::auto_ptr task…
Wolf
  • 9,679
  • 7
  • 62
  • 108
1
vote
2 answers

Difference between pointer and smart pointer

Can you tell me what is wrong with this piece of code? I got asked this in an interview and I am not sure what's wrong with it tClass is a test class with a method printSomething that prints members of tClass. tClass * A = new…
silencer
  • 2,285
  • 5
  • 18
  • 18
1
vote
2 answers

Why does `myvector.push_back(autoPtr.release())` provide the strong exception safety guarantee?

EDIT: I should've mentioned, I was looking at the documentation for Boost's ptr_sequence_adapter and it claims that their adapter for template< class U > void push_back( ::std::auto_ptr x ); is equivalent to doing…
Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
1
vote
1 answer

no warning: ‘auto_ptr’ is deprecated only when auto_ptr used just once

I noticed strange behaviour. It can be narrowed to the following example: #include int main() { std::auto_ptr p1(new int); #ifdef AUTODEP std::auto_ptr p2(new int); #endif } and compilation cpptests$ g++ -Wall…
mborows2
  • 105
  • 1
  • 5
1
vote
2 answers

question about auto_ptr::reset

please can anybody explain this code from C++ Reference site: #include #include using namespace std; int main () { auto_ptr p; p.reset (new int); *p=5; cout << *p << endl; p.reset (new int); *p=10; cout <<…
user466534