auto-ptr

Why is it wrong to use std::auto_ptr<> with STL containers?

Why is it wrong to use std::auto_ptr<> with STL containers? ...

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 ManagedClass { private: NativeClass mNativeClass; // Not allowed ! NativeClass * mNativeCla...

Is it wrong to use auto_ptr with new char[n]

If I declare a temporary auto deleted character buffer using std::auto_ptr<char> buffer(new char[n]); then the buffer is automatically deleted when the buffer goes out of scope. I would assume that the buffer is deleted using delete. However the buffer was created using new[], and so strictly speaking the buffer should be deleted usi...

Using C++ from Objective C : how to allocate/deallocate?

Currently, my Objective C classes use C++ objects by doing a new when the owner is created, and calling delete when it is destroyed. But is there another way? I'd like to be able to declare, say, an auto_ptr whose scope lasts the duration of the Objective C class' lifetime. ...

Will auto_ptr protect against this?

I am not quite clear if auto_ptr will help me in this case: class A { A(const B& member) : _member(B) {}; ... const B& _member; }; A generateA() { auto_ptr<B> smart(new B()); A myA(*smart); return myA; } Will the myA._member reference be valid when smart leaves its enclosing scope? If auto_ptr isn't the answer her...

Re-assinging an "auto_ptr" and Managing Memory

I've a situation like this: class MyClass { private: std::auto_ptr<MyOtherClass> obj; public: MyClass() { obj = auto_ptr<MyOtherClass>(new MyOtherClass()); } void reassignMyOtherClass() { // ... do funny stuff MyOtherClass new_other_class = new MyOtherClass(); // Here, I want to: // 1) Delete the point...

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 know there is std::tr1::shared_ptr. Is there a similar shared_ptr that is in ATL/MFC? ...

Auto Pointer constructor in VC2008

I have an auto pointer implementation: template <typename T, bool Arr = false> class GAutoPtr { T *Ptr; public: typedef GAutoPtr<T, Arr> &AutoPtrRef; GAutoPtr(T *ptr = 0) { Ptr = ptr; } GAutoPtr(AutoPtrRef p) { Ptr = p.Release(); } ~GAutoPtr() { Empty(); } operator T*() { retur...

References and auto_ptr

If I have a auto_ptr I can pass it for a reference?Like: auto_ptr<MyClass>Class(new MyClass); void SetOponent(MyClass& oponent); //So I pass SetOponent(Class) And what is odd copy behavior of auto_ptrs? ...

[C++] Problems with boost::ptr_vector and boost::any

Hey all, ok, so I got a doubt, I want to know if this is possible: I'm using a database, with generic data (strings, ints, bools, etc...). Whenever an object is constructed or a member of an object is modified, I have to query the database with the specific action (SELECT or UPDATE). First of all, this isn't a DB related question, my r...

Returning a new object along with another value

I want to return two values, one of which is a new object. I can do this using std::pair: class A { //... }; std::pair<A*, int> getA() { A* a = new A; //... } To make the code exception-safe, I would like to do: std::pair<std::auto_ptr<A>, int> getA() { std::auto_ptr<A> a(new A); //... } But this won't compile as th...

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; std::string _b; std::auto_ptr< bar > _c; public: foo(const foo& rhs) : _a(rhs._a...

Why does this code only print 42?

Could somebody please explain to me why does this code only print "42" instead of "created\n42"? #include <iostream> #include <string> #include <memory> using namespace std; class MyClass { public: MyClass() {cout<<"created"<<endl;}; int solution() {return 42;} virtual ~MyClass() {}; }; int main(int argc, char *argv[]) { ...

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<char> buffer(new char[sizeBig]); } WinXP sp2, Compiler : BCB.05.03 ...

Returning multiple auto_ptrs from a function

Hello, I have a function that allocates two variables on the heap and returns them to the caller. Something like this: void Create1(Obj** obj1, Obj** obj2) { *obj1 = new Obj; *obj2 = new Obj; } Usually, in similar cases, when I have a function with one variable I use the "source" trick with auto_ptr: auto_ptr<Obj> Create2() ...

When would you use an std::auto_ptr instead of boost::shared_ptr?

We've pretty much moved over to using boost::shared_ptr in all of our code, however we still have some isolated cases where we use std::auto_ptr, including singleton classes: template < typename TYPE > class SharedSingleton { public: static TYPE& Instance() { if (_ptrInstance.get() == NULL) _ptrInstance.rese...

how can I use auto_ptr as member variable that handles another member variable

I have a class like this: class A { private: B* ptr; } But B ptr is shared among different A objects. How can I use auto_ptr so that when A gets destructed B stays on so that other A objects that point to the same ptr can continue without issues. Does this look ok: class A { public: auto_ptr< B > m_Ptr; private: B* ptr; }...

How to effectively delete C++ objects stored in multiple containers? auto_ptr?

I have an application which creates objects of a certain kind (let's say, of "Foo" class) during execution, to track some statistics, and insert them into one or both of two STL maps, say: map<Foo*, int> map1; map<Foo*, int> map2; I was wondering what is the best way to delete the Foo objects. At the moment my solution is to iterate o...

Ternary operator on auto_ptr content not working

I initialize an auto_ptr to NULL and later in the game I need to know if it has NULL or not to return it or a new copy. I've tried this auto_ptr<RequestContext> ret = (mReqContext.get() != 0) ? mReqContext : new RequestContext(); And several other similar stuff casting and so, but g++ tries to invoke auto_ptrs nonexistent operator? (...

Making a non-object resource RAII-compliant

Hello, in my code I use HANDLEs from windows.h. They are used like HANDLE h; if (!openHandleToSomething(arg1, arg2, &h)) { throw std::exception("openHandleToSomething error"); } /* Use the handle in other functions which can throw as well */ if (!CloseHandle(h)) { throw std::exception("closeHandle error"); } As you see, you h...