smart-pointers

should std::auto_ptr<>::operator = reset / deallocate its existing pointee ?

I read here about std::auto_ptr<>::operator= Notice however that the left-hand side object is not automatically deallocated when it already points to some object. You can explicitly do this by calling member function reset before assigning it a new value. However, when I read the source code for header file C:\Program Fil...

Exposing boost::scoped_ptr in boost::python

Hello, I am getting a compile error, saying that the copy constructor of the scoped_ptr is private with the following code snippet: class a {}; struct s { boost::scoped_ptr<a> p; }; BOOST_PYTHON_MODULE( module ) { class_<s>( "s" ); } This example works with a shared_ptr though. It would be nice, if anyone knows the answer. Thank...

What's correct way to remove a boost::shared_ptr from a list?

I have a std::list of boost::shared_ptr<T> and I want to remove an item from it but I only have a pointer of type T* which matches one of the items in the list. However I cant use myList.remove( tPtr ) I'm guessing because shared_ptr does not implement == for its template argument type. My immediate thought was to try myList.remove( sh...

Polymorphic Queue

Hello Everyone, I'm trying to implement a Polymorphic Queue. Here is my trial: QQueue <Request *> requests; while(...) { QString line = QString::fromUtf8(client->readLine()).trimmed(); if(...)){ Request *request=new Request(); request->tcpMessage=line.toUtf8(); ...

C++ smart pointers: sharing pointers vs. sharing data

In this insightful article, one of the Qt programmers tries to explain the different kinds of smart pointers Qt implements. In the beginning, he makes a distinction between sharing data and sharing the pointers themselves: First, let’s get one thing straight: there’s a difference between sharing pointers and sharing data. When yo...

how to cast c++ smart pointer up and down

two clients communicate to each other on top of a message layer in the message body, I need include a field pointing to any data type From client A, I send the field as a shared_ptr<TYPEA> to the message layer. I define this field as a shared_ptr<void> in the message layer. But how can I convert this field back to shared_ptr<TYPEA> in ...

Member variable pointers to COM objects

Hi Folks, Is there any problem with keeping member variable pointer refernces to COM objects and reussing the reference through out the class in C++. Is anybody aware of a reason why you would want to call .CreateInstance every time you wanted a to use the COM object i.e. you were getting a fresh instance each time. I cannot see any re...

Automatically converting an A* into a B*

Hi, Suppose I'm given a class A. I would like to wrap pointers to it into a small class B, some kind of smart pointer, with the constraint that a B* is automatically converted to an A* so that I don't need to rewrite the code that already uses A*. I would therefore want to modify B so that the following compiles... struct A { void...

Pointer to auto_ptr instead of a classical double pointer

Hello. I'm quite new to smart pointers and was trying to refactor some existing code to use auto_ptr. The question I have is about double pointers and their auto_ptr equivalent, if that makes sense. I have a function that accepts a double pointer as its parameter and the function allocates resources for it: void foo ( Image** img ) { ....

How to attach boost::shared_ptr (or another smart pointer) to reference counter of object's parent?

I remember encountering this concept before, but can't find it in Google now. If I have an object of type A, which directly embeds an object of type B: class A { B b; }; How can I have a smart pointer to B, e. g. boost::shared_ptr<B>, but use reference count of A? Assume an instance of A itself is heap-allocated I can safely get ...

How to enforce users to create objects of class derived from mine with "new" only?

To implement reference counting we use an IUnknown-like interface and a smart pointer template class. The interface has implementation for all the reference-count methods, including Release(): void IUnknownLike::Release() { if( --refCount == 0 ) { delete this; } } The smart pointer template class has a copy constructor an...

How to handle failure to release a resource which is contained in a smart pointer?

How should an error during resource deallocation be handled, when the object representing the resource is contained in a shared pointer? EDIT 1: To put this question in more concrete terms: Many C-style interfaces have a function to allocate a resource, and one to release it. Examples are open(2) and close(2) for file descriptor...

C++ smart pointer for a non-object type?

Hi, I'm trying to use smart pointers such as auto_ptr, shared_ptr. However, I don't know how to use it in this situation. CvMemStorage *storage = cvCreateMemStorage(); ... use the pointer ... cvReleaseMemStorage(&storage); I'm not sure, but I think that the storage variable is just a malloc'ed memory, not a C++ class object. Is there...

Can I get a raw pointer from boost's weak_ptr?

Is it possible to get a raw pointer from boost::weak_ptr? Boost's shared_ptr has get() method and "->" operator. Is there some rationale behind weak_ptr not having the same functionality? ...

CComPtr CoCreateInstance returns 0x80070582 (Class already exists.)

I have a StartComObjects function called when the user presses the Login button and a StopComObjects function called when the user presses the Cancel button. The StartComObjects function uses CComPtr.CoCreateInstance to create the COM object and sets up some connection points using AfxConnectionAdvise. When the user presses the Cancel ...

Where is shared_ptr?

I am so frustrated right now after several hours trying to find where shared_ptr is located. None of the examples I see show complete code to include the headers for shared_ptr (and working). Simply stating std, tr1 and <memory> is not helping at all! I have downloaded boosts and all but still it doesn't show up! Can someone help me by ...

Why can operator-> be overloaded manually?

Wouldn't it make sense if p->m was just syntactic sugar for (*p).m? Essentially, every operator-> that I have ever written could have been implemented as follows: Foo::Foo* operator->() { return &**this; } Is there any case where I would want p->m to mean something else than (*p).m? ...

Throwing Exception in CTOR and Smart Pointers

Is it OK to have the following code in my constructor to load an XML document into a member variable - throwing to caller if there are any problems: MSXML2::IXMLDOMDocumentPtr m_docPtr; //member Configuration() { try { HRESULT hr = m_docPtr.CreateInstance(__uuidof(MSXML2::DOMDocument40)); ...

C++0x unique_ptr replaces scoped_ptr taking ownership?

I used to write code like this: class P {}; class Q: public P {}; class A { // takes ownership A(P* p): p_(p) {} scoped_ptr<P> p_; }; A a(new Q); With C++0x, should I rewrite class A as: class A { // takes ownership A(unique_ptr<P>&& p): p_(move(p)) {} unique_ptr<P> p_; }; ...

How do I create a duplicate instance of object contained in a shared pointer in c++?

I have an object which has both a copy constructor and assignment operator defined. It is enclosed inside a shared pointer. I want to make another shared pointer that contains a copy of the original shared pointer (i.e. new shared pointer to a new memory location, which however, has the same data as the original object). Thanks for an...