Sign in to follow this  

looking to move to boost::shared_ptr and boost:intrinsic_ptr

Recommended Posts

I have been looking at using these two templates for a while, but have been unable to grasp how to use them properly. Here are my questions: 1) I know how to create one eg. boost::shared_ptr<int> somePtr; what if I want to create a pointer to some class with a constructor that takes some parameters? 2) When I am done with the pointer, how do I tell boost::shared_ptr that I am done with it? 3) can i pass it by reference properly or as a pointer into a function? 4) if boost::shared_ptr is pointing to a class/struct, how do i call a function, or set a member variable from that class/struct 5) I remember that boost::intrinsic_ptr can be used with DirectX to call release() afterwards instead of delete on the templated object. Is there any online tutorial or documantation that demonstrates how to use it because this is probably all a repost and it would be good to have. I looked in the articles section, but did not see anything there. Thanks in advance, ~guyaton

Share this post

Link to post
Share on other sites
1) boost::shared_ptr<Something> some_ptr; only creates a pointer, it doesn't create the object. To create an object you generally use new. ex:

boost::shared_ptr<Something> some_ptr(new Something(some_parameter));

2) When you're done with the pointer just let it go out of scope normally. It's destructor should take care of things.

3) Generally you pass boost::shared_ptr by value.

4) You call functions and access like you would with normal pointers. ex:

5) boost::shared_ptr also can take a deleter object or function to be called instead of delete too.

The boost::shared_ptr documents at the boost website should cover most of the usage points.

Share this post

Link to post
Share on other sites
It's not called intrinsic_ptr, it's called intrusive_ptr [lol].

Anyways as it says on the tin, it's an "intrusive" solution to embedded reference count, boost::intrusive_ptr is a smart pointer for objects that already maintain an embedded reference count like COM objects therefore DX objects and is therefore more efficient than using boost::shared_ptr plus some custom delete function/functor but remember it's an intrusive solution.

To use boost::intrusive_ptr you just need to define the free-functions intrusive_ptr_add_ref and intrusive_ptr_release for the raw pointer type, either in the namespace that corresponds to their parameter for more standard compliant compilers that supports argument-dependent lookup (AKA koenig Lookup) otherwise the definitions need to go in namespace boost (open-up the namespace then close it again).

[Edited by - snk_kid on October 23, 2005 6:26:20 PM]

Share this post

Link to post
Share on other sites
What if I want to create the boost::shared_ptr as a member variable and create it later? I tried this:
boost::shared_ptr<CQuaternionCamera> m_pCamera;

and later in my code:

m_pCamera = new CQuaternionCamera( EYE, LOOKAT, UP, this->m_pD3DDevice );

the constructor looks like this:

CCamera( D3DXVECTOR3 &vecEye, D3DXVECTOR3 &vecLookat, D3DXVECTOR3 &vecUp, LPDIRECT3DDEVICE9 D3DDevice );

but get this error:
error C2679: binary '=' : no operator found which takes a right-hand operand of type 'CQuaternionCamera' (or there is no acceptable conversion)

and I have this error:

this->m_pInputControl.SetCamera( this->m_pCamera );

where the function call looks like:

void SetCamera( CQuaternionCamera &cam ) { this->m_pQuatCam = &cam; }

error C2664: 'CInputControl::SetCamera' : cannot convert parameter 1 from 'boost::shared_ptr<T>' to 'CQuaternionCamera *'


It's not called intrinsic_ptr, it's called intrusive_ptr

snk_kid: oops sorry bout that.


Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this