Jump to content
  • Advertisement
Sign in to follow this  
JimmyDeemo

Boost smart pointer confusion.

This topic is 3694 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok so i have the following in my project;
CInputHandler* m_pInput;

//Somewhere else in the code i pass it to the camera class
void CCamera::Update(float dt, const CInputHandler* input)
{
}

Update( dt, m_pInput );

I'd like to turn the pointer in a boost scoped_ptr, as it is only set once and shouldn't change. How can i pass it to a function if i do that?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by JimmyDeemo
Ok so i have the following in my project;

*** Source Snippet Removed ***

I'd like to turn the pointer in a boost scoped_ptr, as it is only set once and shouldn't change. How can i pass it to a function if i do that?
I haven't used scoped_ptr much, so I may not get this exactly right, but I think you could rewrite your example as:

typedef boost::scoped_ptr<InputHandler> InputHandlerPtr;
InputHandlerPtr m_input;

// ...

void Camera::Update(float dt, const InputHandlerPtr& input) {...}

// ...

Update(dt, m_input);

You may find though that scoped_ptr isn't the right tool for the job (depending on how exactly m_input is intended to be used). Just remember that unlike (e.g.) shared_ptr, scoped_ptr assumes exclusive ownership of the pointed-at object (this means, among other things, that you can't make copies of scoped_ptr objects).

Share this post


Link to post
Share on other sites
Quote:
You can get the contained raw pointer via the T* get() method.
Why would you want to do that? In most cases, invoking the get() method is unnecessary, and defeats the purpose of using a smart pointer in the first place.

Share this post


Link to post
Share on other sites
Well i'd really just like to be smart (pun intended :D) about my pointer selection.

What would be an appropriate one to use? I want the same behaviour, in the sense that i'd like to be able to pass my pointer to other functions and (in my case using const) state that i don't want anything to modify it.

I will certainly try the method detailed above but it seems like a funny way to do it. Maybe scoped_ptr isn't what i need?

Share this post


Link to post
Share on other sites
Quote:
Original post by JimmyDeemo

What would be an appropriate one to use? I want the same behaviour, in the sense that i'd like to be able to pass my pointer to other functions and (in my case using const) state that i don't want anything to modify it.


What is 'CInputHandler* m_pInput;'?

Share this post


Link to post
Share on other sites
Quote:
Original post by jyk
Quote:
You can get the contained raw pointer via the T* get() method.
Why would you want to do that? In most cases, invoking the get() method is unnecessary, and defeats the purpose of using a smart pointer in the first place.

There are plenty of cases where using get() does not defeat the purpose of using smart pointers in the first place.

1) Interop with pointer using libraries
2) Passing to code which treats pointers as (nullable) references (e.g. code which temporarily accesses an object and should not affect the lifetime of an object)

I doubt his Update() stores the pointer in question, given that you presumibly call the function again every frame, making this seem like a poster child for case #2. Now, granted, I'd probably push for Update to take a reference instead of a pointer in this case, but it's hardly defeating the purpose of smart pointers: which is to manage object lifetime for you. Since the method in question has no effect or dealings with that lifetime, making Update take, say, a const boost::scoped_ptr<T>&, does nothing but increase coupling (bad) for no gain.

Share this post


Link to post
Share on other sites
Quote:
Original post by Antheus
What is 'CInputHandler* m_pInput;'?


It is a pointer to an object that has information about the keyboard and mouse. Such as key's pressed, mouse buttons and distance mouse moved.

I the camera class to access the information so it can update the view matrix based upon input from the keyboard and mouse.

Sorry if i wasn't clear enough.

Edit: Would making it a constant reference (as suggested above) make more sense? Basically the camera class needs to access the class but not make any changes.

Share this post


Link to post
Share on other sites
Quote:
Original post by MaulingMonkey
Quote:
Original post by jyk
Quote:
You can get the contained raw pointer via the T* get() method.
Why would you want to do that? In most cases, invoking the get() method is unnecessary, and defeats the purpose of using a smart pointer in the first place.

There are plenty of cases where using get() does not defeat the purpose of using smart pointers in the first place.

1) Interop with pointer using libraries
2) Passing to code which treats pointers as (nullable) references (e.g. code which temporarily accesses an object and should not affect the lifetime of an object)

I doubt his Update() stores the pointer in question, given that you presumibly call the function again every frame, making this seem like a poster child for case #2. Now, granted, I'd probably push for Update to take a reference instead of a pointer in this case, but it's hardly defeating the purpose of smart pointers: which is to manage object lifetime for you. Since the method in question has no effect or dealings with that lifetime, making Update take, say, a const boost::scoped_ptr<T>&, does nothing but increase coupling (bad) for no gain.
If coupling is a concern (and the argument in question is not optional), then I would recommend that the OP use a reference (as you yourself mentioned in your post).

I'm quite aware of your level of technical expertise, MaulingMonkey, so I certainly don't intend to argue with you :) However, I have to say that my opinion remains more or less unchanged: that aside, perhaps, from interoperating with third-party APIs that expect raw pointers, use of the Boost smart pointer get() functions should be rare (at the very least, it should be the exception and not the rule).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!