smart-pointers

C++ using scoped_ptr as a member variable

Just wanted opinions on a design question. If you have a C++ class than owns other objects, would you use smart pointers to achieve this? class Example { public: // ... private: boost::scoped_ptr<Owned> data; }; The 'Owned' object can't be stored by value because it may change through the lifetime of the object. My view of it ...

What is the best way to implement smart pointers in C++?

I've been evaluating various smart pointer implementations (wow, there are a LOT out there) and it seems to me that most of them can be categorized into two broad classifications: 1) This category uses inheritance on the objects referenced so that they have reference counts and usually up() and down() (or their equivalents) implemented....

getting a normal ptr from shared_ptr ?

i have something like shared_ptr t(makeSomething(), mem_fun(&Type::deleteMe)) i now need to call C styled func that require a pointer to Type. How do i get it from shared_ptr? ...

Problems writing a copy constructor for a smart pointer

The code I'm working with has its own smart pointer implementation which does simple reference counting. Yes, we shouldn't have our own implementation. Yes, we should be using one from boost or some such. Bear with me. I found I wanted to write code like this: ... CountedPointer<Base> base; ... CountedPointer<Derived> derived; ... base...

smart pointers (boost) explained

What is the difference between the following set of pointer? When do you use each pointer in a production code, if at all? Examples would be appreciated! 1.scoped_ptr 2.shared_ptr 3.weak_ptr 4.intrusive_ptr Edit#1 Do you guys use boost in production code? ...

Smart Pointers with "this" in C++

I have been working on replacing raw pointers with reference-counted pointers that expose only a const version of the underlying one. My objective is to reduce memory usage (and time spent unnecessarily constructing and destructing complex objects) without putting myself in a situation where any code has access to memory that it does no...

Help me make this code exception-safe.

So I have this library code, see... class Thing { public: class Obj { public: static const int len = 16; explicit Obj(char *str) { strncpy(str_, str, len); } virtual void operator()() = 0; private: char str_[len]; }; explicit Thing(vector<Obj*> &o...

Pointer to a Pointer question

I have a class with a (non smart) pointer to an interface object (lets call it pInterface) and I am building a nested class which also needs access to that interface. I am going to get around this by passing the pointer to the interface into the constructor of the nested class like so: CNestedClass someClass( pInterface, ... ); Howeve...

STL class for reference-counted pointers?

This should be trivial but I can't seem to find it (unless no such class exists!) What's the STL class (or set of classes) for smart pointers? UPDATE Thanks for the responses, I must say I'm surprised there's no standard implementation. I ended up using this one: http://www.gamedev.net/reference/articles/article1060.asp ...

Passing a smart pointer as argument inside a class: scoped_ptr or shared_ptr ?

I have a class that creates an object inside one public method. The object is private and not visible to the users of the class. This method then calls other private methods inside the same class and pass the created object as a parameter: class Foo { ... }; class A { private: typedef scoped_ptr<Foo> FooPtr; void pri...

How to enable_shared_from_this of both parend and derived

Hello, I have simple base and derived class that I want both have shared_from_this(). This simple solution: class foo : public enable_shared_from_this<foo> { void foo_do_it() { cout<<"foo::do_it\n"; } public: virtual function<void()> get_callback() { return boost::bind(&foo::foo_do_it,shared_from_this()); } virtual ~foo() {}...

A Large Number of sp_counted_impl_p Objects

I just performed Allocation Profiling about how many objects of each type are in my application. I am using boost::shared_ptr extensively. I found a large number of sp_counted_impl_p objects allocated, each occupying 16 bytes. How many of sp_counted_impl_p objects can be expect per shared_ptr? Does someone have an idea? ...

How does a reference-counting smart pointer's reference counting work?

In other words, how does the implementation keeps track of the count? Is there a map-like object maintained which is accessible by all the shared_ptr instances whose key is the pointer's address and value is the number of references? If I've to implement a shared_ptr, this is the first idea that's coming to my mind. Is there a possibil...

Smart pointers/safe memory management for C?

Hi, I, and I think many others, have had great success using smart pointers to wrap up unsafe memory operations in C++, using things like RAII, et cetera. However, wrapping memory management is easier to implement when you have destructors, classes, operator overloading, et cetera. For someone writing in raw C99, where could you point ...

Is there a smart pointer that is automatically nulled when its target is destroyed in C++

I've found QPointer. Are there any others? ...

Can smart pointers selectively hide or re-direct function calls to the objects they are wrapping?

I'm working on a project where certain objects are referenced counted -- it's a very similar setup to COM. Anyway, our project does have smart pointers that alleviate the need to explicitly call Add() and Release() for these objects. The problem is that sometimes, developers are still calling Release() with the smart pointer. What I'm...

best practice when returning smart pointers

What is the best practice when returning a smart pointer, for example a boost::shared_ptr? Should I by standard return the smart pointer, or the underlying raw pointer? I come from C# so I tend to always return smart pointers, because it feels right. Like this (skipping const-correctness for shorter code): class X { public: boost::...

How to maintain a weak pointer to a parent in C++?

Is there a standard way of maintaining a weak pointer to a parent (which is created using a shared pointer) in a child object in C++? Essentially, I need to implement something on the lines of the following: Class B; Class A { ... private: B m_b; }; Class B { .... public: void SetParentPtr(const boost::shared_ptr<A>& a) { m_parentP...

Covariant virtual functions and smart pointers

In C++, a subclass can specify a different return type when overriding a virtual function, as long as the return type is a subclass of the original return type (And both are returned as pointers/references). Is it possible to expand this feature to smart pointers as well? (Assuming a smart pointer is some template class) To illustrate:...

Smart pointers. When, where and how?

First off, since there are different kinds of smart pointers, I'd like to focus this question on two of them: reference counted intrusive and non-intrusive smart pointers. The question is asked individualy for each pointer type. I am not really sure how to formulate my question, so here's what I'm not asking: I am not asking why, or whe...