smart-pointers

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...

Is there a boost smart pointer class that can be configured not to delete at destruction?

I have a list of smart pointers. I want some of these smart pointers to act as regular pointers, meaning they are simply a reference to an instance and are not involved in its deallocation. They might for example point to instances allocated on the stack. The other smart pointers in the list should act as regular boost::shared_ptr. He...

How to implement smart pointer which can be instantiated with void?

Some smart pointer templates, such as boost::shared_ptr, may be instantiated with void to hold an arbitrary object: http://www.boost.org/doc/libs/1_39_0/libs/smart_ptr/sp_techniques.html#pvoid Below is a minimal scoped_ptr implementation. When instantiated with void, the compiler complains about an illegal "reference to void" being for...

Implementing Smart Pointer - Dynamic Allocation with templates

I'm in the process of writing a smart pointer countedptr and I've hit a speed bump. The basic function of countedptr is to work like any other smart pointer and also have a count of how many pointers are pointing to a single object. So far, the code is: [SOLVED] #include "std_lib_facilities.h" template <class T> class counted_ptr{ pri...

Implementing Smart Pointer - storing template class in vector

I'm having trouble storing instances of my smart pointer into a container. Here is the code for the pointer. #include "std_lib_facilities.h" template <class T> class counted_ptr{ private: T* pointer; int* count; public: counted_ptr(T* p = 0, int* c = new int(1)) : pointer(p), count(c) {} // default constructor expli...

Should the "this" pointer and smart pointers be mixed?

How should I avoid using the "this" pointer in conjunction with smart pointers? Are there any design patterns/general suggestions on working around this? I'm assuming combining the two is a no-no since either: you're passing around a native pointer to a smart pointer-managed object which defeats the point of using the smart pointers ...

How should smart pointers get down casted?

Do smart pointers handle down casting, and if not what is a safe way of working around this limitation? An example of what I'm trying to do is having two STL vectors (for example) containing smart pointers. The first contains smart pointers to a base class while the second contains smart pointers to a derived class. The smart pointers...

Should boost::ptr_vector be used in place std::vector all of the time?

Just a conceptual question that I've been running into. In my current project it feels like I am over-using the boost smart_ptr and ptr_container libraries. I was creating boost::ptr_vectors in many different objects and calling the transfer() method to move certain pointers from one boost::ptr_vector to another. It is my understandin...

shared_ptr and references in C++

References in C++ are a conveneint construct that allow us to simplify the following C code: f(object *p){ //do something } int main(){ object* p = (object*) calloc(sizeof(object)); f(p); } to f(object& o){ //do something } int main(){ object o = object(); f(o); } Shared pointers are another convenience in C++ that s...

What is boost's shared_ptr(shared_ptr<Y> const & r, T * p) used for?

boost::shared_ptr has an unusual constructor template<class Y> shared_ptr(shared_ptr<Y> const & r, T * p); and I am a little puzzled as to what this would be useful for. Basically it shares ownership with r, but .get() will return p. not r.get()! This means you can do something like this: int main() { boost::shared_ptr<int> x(ne...

C++ and Smart Pointers - how would smart pointers help in this situation?

Much to my shame, I haven't had the chance to use smart pointers in actual development (the supervisior deems it too 'complex' and a waste of time). However, I planned to use them for my own stuff... I have situations regarding de-initing a module after they are done, or when new data is loaded in. As I am using pointers, I find my code...

Why is my code slower using #import "progid:typelib" than using "MFC Class From TypeLib"?

I am writing an automation client in Visual C++ with MFC. If I right-click on my solution » Add » Class, I have the option to select MFC Class From TypeLib. Selecting this option generates source/header files for all interfaces. This allows me to write code such as: #include "CApplication.h" #include "CDocument.h" // ... connect to ...

Smart pointers in Qt

Like it has been written here Qt up to now has 8 specilized smart pointer classes. It looks like it is all you will ever need. However, in order to use any of these smart pointers your class must be derived from QObject which is not always convenient. Is there other implementations of smart pointers in Qt which work with arbitrary classe...

C++ Initialization list and memory alloc.

Hi, Is the following valid? class myClass { private: ... int m_nDataLength; boost::shared_array<int> m_pData; ... public: myClass(): ..., m_nDataLength(10), m_pData(new int[m_nDataLength]), ... { } } Am I right in assuming that the initialization will happen exactly in the order I've given in ...

Is it possible for slicing to occur with Smart Pointers?

If I understand slicing correctly I don't think this could happen with pointers or smart pointers. For example, if you had: class A { int something; }; class B : public A { int stuff; int morestuff; }; int main() { std::shared_ptr<B> b(new B()); std::shared_ptr<A> a; a = b; } My understanding is that the block of memory ...

Using Smart Pointers with boost intrusive

I am having trouble using smart pointers in boost intrusive containers. According to the documentation the requirements for using smart pointers with intrusive containers are the following: It must support the same operations as a raw pointer, except casting. It must be convertible to a raw pointer and constructible from a raw pointer...

shared_ptr vs scoped_ptr

scoped_ptr is not copy able and is being deleted out of the scope. So it is kind of restricted shared_ptr. So seems besides the cases when you really need to restrict the copy operation shared_ptr is better to use. Because sometimes you don’t know you need to create a copy of your object or no. So the question is: besides the cases menti...

Conditionally instantiate a template at run-time.

I have a template class template <class T> class myClass { public: /* functions */ private: typename T::Indices myIndices; }; Now in my main code I want to instantiate the template class depending on a condition. Like : myFunc( int operation) { switch (operation) { case 0: // Ins...

How to avoid memory leak with boost::shared_ptr?

Consider the following code. using boost::shared_ptr; struct B; struct A{ ~A() { std::cout << "~A" << std::endl; } shared_ptr<B> b; }; struct B { ~B() { std::cout << "~B" << std::endl; } shared_ptr<A> a; }; void main() { shared_ptr<A> a (new A); shared_ptr<B> b (new B); a->b = b; b->a = a; } There is ...

Cast boost::shared_array<char> to boost::shared_array<const char>

How can I cast a boost::shared_array<char> to boost::shared_array<const char>? ...