[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
CppSharedPtr

(C++) boost::shared_ptr

(same as tr1::shared_ptr)

A smart pointer from the Boost C++ Library that automatically deletes the instance it is pointing to, when it goes out of scope.

  1. include <boost/shared_ptr.hpp>
int main() { const boost::shared_ptr<MyClass> myClass(new MyClass); return 0; }


The difference with std::auto_ptr is, that a copy of a shared pointer is symmetrical. Due to this, a shared_ptr can be put in a vector.

  1. include <iostream>
  2. include <stdlib>
  3. include <vector>
  4. include <boost/shared_ptr.hpp>
class MyClass { public: MyClass(const int& value) : mValue(value) { ; } void cout() const { std::cout << mValue << std::endl; } private: const int mValue; }; int main() { const int size = 10; std::vector< boost::shared_ptr<MyClass> > myVector(size); for (int i=0; i!=size; ++i) myVector[i].reset(new MyClass(i)); std::cout << "Before shuffling: " << std::endl; for (int i=0; i!=size; ++i) myVector[i]->cout(); std::random_shuffle(myVector.begin(),myVector.end()); std::cout << "After shuffling: " << std::endl; for (int i=0; i!=size; ++i) myVector[i]->cout(); }


Internally, a shared_ptr keeps track how many copies of the pointer are made, only calling delete when the last one goes out of scope.

shared_ptr is not in the STL, but in the Boost C++ Library and TR1.

Nasty compile error with boost::shared_ptr

[C++ Error] shared_ptr.hpp(124): E2034 Cannot convert 'Y *' to 'BirdBase *'
[C++ Error] shared_count.hpp(82): E2450 Undefined structure 'sp_counted_impl_p<Y>'
[C++ Error] shared_count.hpp(82): E2315 'sp_counted_impl_p<Y>()' is not a member of 'sp_counted_impl_p<Y>', because the type is not yet defined
[C++ Error] shared_count.hpp(82): E2034 Cannot convert 'sp_counted_impl_p<Y> *' to 'sp_counted_base *'
[C++ Error] checked_delete.hpp(32): E2034 Cannot convert 'Y' to 'bool'


caused by:

//Removes the boost::shared_ptr<BirdBase> mMale from the class Couple
boost::shared_ptr<BirdBase> Couple::getMale()
{
  boost::shared_ptr<BirdBase> male(mMale);
  mMale.reset(0);
  return male;
}


The compile error is solved by changing this to:

//Removes the boost::shared_ptr<BirdBase> mMale from the class Couple
boost::shared_ptr<BirdBase> Couple::getMale()
{
  boost::shared_ptr<BirdBase> male(mMale);
  mMale.reset(); //Remove the 0
  return male;
}


shared_ptr.hpp(253): Call to undefined function 'assert'

I don't know why, but I've got this error in the following section of code, in the second line.

T * operator-> () const // never throws
{
    BOOST_ASSERT(px != 0);
    return px;
}


I still do not understand why it occurs (as on top of the header file boost/assert.hpp is #included.

This error started to occur when I split a multi-unit project over multiple folders, in which I had defined by own assert.h and assert.cpp files. When I
  • removed the Forms from 'Project | Options | Forms' from 'Auto-Create Forms' to 'Available', compiled successfully (!) and set the Forms back to their desired availability.
  • removed all files from the project
  • removed all project directories (i.e. the multiple paths for the project)
  • renamed my assert files to ThorAssert.h and ThorAssert.cpp
  • added the files to the project again (with the help of 'Unresolved external errors')
  • #included the newly named assert files
It worked again in mysterious ways...

[C++ Error] shared_ptr.hpp(162): E2034 Cannot convert 'Base * const' to 'Derived *'

The reason for this error is exactly as stated. The main problem is that the compiler does not take you to the line which causes it. It is caused by a line in which you try to use a base class for e.g. a function taking a derived class. An example is the code below (the full code is at inheritance).

  1. include <boost/shared_ptr.hpp>
//--------------------------------------------------------------------------- struct Base { Base(const int& value) : mValueBase(value) {} const int mValueBase; }; //--------------------------------------------------------------------------- struct Derived : public Base { Derived(const int& value1, const int& value2) : Base(value1), mValueDerived(value2) {} const int mValueDerived; }; //--------------------------------------------------------------------------- void funcDerived(const boost::shared_ptr<Derived> & derived) { //Does nothing derived; } //--------------------------------------------------------------------------- int main() { boost::shared_ptr<Base> base(new Base(0)); funcDerived(base); //This line causes the compile error }


Code links

Links



last edited (May 8, 2008) by bilderbikkel, Number of views: 7434, Current Rev: 15 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.