smart-pointers

Make any class reference counted with inheritance?

In my new project I wish to (mostly for to see how it will work out) completely ban raw pointers from my code. My first approach was to let all classes inherit from this simple class: template class Base { public: typedef std::shared_ptr ptr; }; And simple use class::ptr wherever I need a pointer. This ap...

Correct way to initialize array of boost::scoped_ptr ?

I have a class with an array of scoped pointers to objects which do NOT have a default constructor. The only way I've found to "initialise" them is using swap() like this: class Bar { Bar(char * message) {}; } class Foo { boost::scoped_ptr<Bar> arr[2]; Foo() { arr[0].swap(boost::scoped_ptr<Bar>( new Bar("ABC") )); ar...

question about auto_ptr::reset

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

Problem with weak_ptr comparison in VS10

I can not get 'operator <' to compile for a weak_ptr using VS10. Am I missing an #include or #using? Even the the code sample in the documentation does not work for me. http://msdn.microsoft.com/en-us/library/bb982759.aspx // temp.cpp : Defines the entry point for the console application. // #include "stdafx.h" // std_tr1__memory__o...

C++ "smart pointer" template that auto-converts to bare pointer but can't be explicitly deleted

I am working in a very large legacy C++ code base which shall remain nameless. Being a legacy code base, it passes raw pointers around all over the place. But we are gradually trying to modernize it and so there are some smart pointer templates as well. These smart pointers (unlike, say, Boost's scoped_ptr) have an implicit conversion...

Passing smart pointer to the function (which acccepts void*) without calling destructor of pointee.

Hello: I have my own implementation of smart pointer which uses reference counting as ownership mechanism (Note: I have tested it and it has no bugs). Following is my code flow. Create Object and create Smart pointer to the object Call function which has following defination : void Func(void* param) (Note: This function run in diff...

Exception within function returning value for constructor...

Let's say I have class that acts as a "smart pointer" and releases some kind of system resource when destroyed. class Resource{ protected: ResourceHandle h; public: Resource(ResourceHandle handle) :h(handle){ } ~Resource(){ if (h) releaseResourceHandle(h);//external function, probably from...

When is it appropriate to use C++ Smart Pointers in GUIs (Programs with main loops)

Hi friends. I am leaning towards using std::tr1::shared_ptr to automatically manage a pointer to a utility class in my GUI program. Basically here's a skeleton of the program: int main () { Allocate dynamic memory for utility class GUI code.. GUI code... GUI Code.. GUI Main Loop } The program finishes when the user ca...

Besides Boost, where can I get a single-file smart pointer implementation for C++?

I want to start using smart pointers in my code but I don't really want to use Boost because it's so huge. Can anyone recommend a simple, one-file smart pointer implementation? Thanks, Boda Cydo. ...

Boost shared_ptr: How how to use custom deleters and allocators

Free function allocate_shared can be used with any standard compliant allocator. But what about shared_ptr's constructor and reset method. template<class Y, class D, class A> shared_ptr(Y * p, D d, A a); template<class Y, class D, class A> void reset(Y * p, D d, A a); The manual says that D should provide a call operator which will be...

Accelerated C++ - Ch 13 & 14 - Is a handle the same thing as a smart pointer?

I'm just about done Koenig & Moo's Accelerated C++ and in Chapters 13 & 14 they lay out the idea and implementation of a few Handle classes (simple, shared, reference counted). The classes encpasulate a raw pointer and abstract the allocation / deallocation of dynamic objects away from the client code to avoid all the dangers of raw poi...

std::auto_ptr to std::unique_ptr

With the new standard coming (and parts already available in some compilers). The new type std::unique_ptr is supposed to be a replacement for std::auto_ptr. Does their usage exactly overlap (so I can do a global find/replace on my code (not that I would do this, but if I did)) or should I be aware of some differences that are not ap...

Example to use shared_ptr ?

Hi I asked a question today about How to insert different types of objects in the same vector array and my code in that question was gate* G[1000]; G[0] = new ANDgate() ; G[1] = new ORgate; //gate is a class inherited by ANDgate and ORgate classes class gate { ..... ...... void Run() { //A virtual function } }; class ANDga...

Can anyone give some hints or point to a tutorial on how to extract just one specific piece from Boost?

I want to use smart pointers in my code but I can't figure out how to take them out from the Boost. Can anyone give some hints on how to extract things from Boost so that they can be used individually? Thanks, Boda Cydo. ...

Is it possible to use a C++ smart pointers together with C's malloc?

Some of my code still uses malloc instead of new. The reason is because I am afraid to use new because it throws exception, rather than returning NULL, which I can easily check for. Wrapping every call to new in a try{}catch(){} also doesn't look that good. Whereas when using malloc I can just do if (!new_mem) { /* handle error */ }. Th...

What is the right way to put a smart pointer in class data (as class member) in C++?

Suppose I have a class Boda: class Boda { ... }; And I have a member cydo in this class that I want to be a smart pointer (that is, I want it to get deallocated automatically as soon as the class gets destroyed). I am using Boost's smart pointers, so I write: class Boda { boost::shared_ptr<int> cydo; public: Boda...

How to use C++ Smart Pointers?

I've been using C++ for some time now and I still don't feel very comfortable about using smart pointers and I've only been using them when editing some code that uses them, never in my own code (it might be worth to say that I'm a student). Can you explain what are the types of smart pointers, how do they work and when to use them? Al...

How can I prevent storing an intrusive_ptr-based class in other smart pointers

At work we have a base class, let's call it IntrusiveBase, that acts something like a mixin to allow a class to be stored in a boost:intrusive_ptr. That is, it provides its subclasses with a ref count and defines intrusive_ptr_add_ref and intrusive_ptr_release overloads. The problem is that it is too easy for someone to forget that a par...

enable_shared_from_this and objects on stack

What about calling shared_from_this for stack allocated objects ? enable_shared_from_this in base classes list is indicator for user of derived class for creating it's only on heap (and we sholuld just hope for correct class usage) or we can have some more strong protection against such errors ? Or I don't understand some moments ? Examp...

boost::shared_ptr cycle break with weak_ptr

I am currently in a situation like: struct A { shared_ptr< B > b; } struct B { shared_ptr< A > a; } shared_ptr< A > a(new A()); shared_ptr< B > b(new B(); a->b(b); b->a(a); I know this won't work, because the references would continue to point to each other. I've also been told that weak_ptr solves this issue. However, weak ptr has no...