# Getting a down-casted boost::shared_ptr?

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

## Recommended Posts

Is it possible? I'm making a simple resource manager. I want it to return a shared_ptr to the specific type of resource, but the internal list is of the parent type. So is it possible for shared_ptr to return a specialized pointer without messing the reference count?
class asset
{
};

class image : public asset
{
};

class asset_manager
{
private:
std::map< std::string, boost::shared_ptr< asset > > list;
...
public:
...
boost::shared_ptr< asset > get_resource( const std::string & );
...
};

int main()
{
asset_manager m;
...
boost::shared_ptr< image > i = m.get_resource( "image_01" );
}


I know it's RTTI stuff, but in this case I don't think of another way, since these assets will all work differnetly and I'd like to keep em in one place. I would know how to do it by returning a simple pointer and using some RTTI or type recognition inside the get_resource method, but is there a way to return a shared_ptr without messing the refernece count, that's my question?

##### Share on other sites
Hi,

k, I'll try something here...

If you use a shared_ptr inside the get_resource method, I think the object will be gone after the method is done: either the shared_ptr it was assigned to determines that it should delete the object, or any casting of the containing type will remove the ref counting information.

I think (not know [wink]) the best way is to use an ordinairy pointer inside the get_resouce method, but return is as a shared_ptr. The last thing is important since that's the way you keep the object alive during transition from method to callee.

hth somewhat [wink],
CipherCraft
(Not having his best day)

##### Share on other sites
It's entirely possible, except that you'll need to do an appropriate cast:
boost::shared_ptr< image > i = dynamic_pointer_cast<image>(m.get_resource( "image_01" ));

You can read up more about dynamic_pointer_cast and static_pointer_cast in shared_ptr docs. Also, I strongly suggest using typedefs like this:
typedef boost:shared_ptr<image> image_ptr;

This will make your life significantly easier and can mean the difference between pleasure and incredible frustration [smile]

##### Share on other sites
Thanks CoffeeMug, that was the exact explanation I was looking for, I had not found it in boost's documentation yet, so I was at a loss. I knew it was possible for simple pointer types, but didn't know there was an equivalent for boost smart pointers.

About typedefs, yeah, I'll be sure to use them, it was just for simplification of my example that I didn't use them.

##### Share on other sites
Duh!

hmmmmm, that nick inspired me: I'm gonna get myself a cuppa and try to wake up.

cu,
CipherCraft

##### Share on other sites
Before you go rush into using dynamic_pointer_cast [grin], as with dynamic_cast the type must be a polymorphic type meaning the base must define at least 1 virtual member function to work. I thought i would mention it as i couldn't see any in the posted code just in case.

##### Share on other sites
[rant]

Does anyone else find it unfortunate that C++ allows runtime type identification using typeid and dynamic_cast but doesn't allow runtime type generation? Lack of the latter makes the former significantly less useful in some situations. If you ever find yourself with a rich object hierarchy in which some types occassionally behave differently at runtime (a behavior you cannot predict at compile time) you can no longer use typeid and must come up with your own method of type identification. For example:
struct BaseType{    virtual int get_type_id() = 0;    }struct DerivedType1 : public BaseType { ... };

Since there is no way to generate DerivedType1 at runtime and there is no real way to modify typeid you're stuck with having to use get_type_id. Do I make any sense?

[/rant]

##### Share on other sites
<snip>
Quote:
 Original post by CoffeeMugSince there is no way to generate DerivedType1 at runtime and there is no real way to modify typeid you're stuck with having to use get_type_id. Do I make any sense?

That is a problem C++ has.. and in every big project I have been working on, we have used our "own RTTI" for that, and other reasons.

D seems like a nice language. Wonder if it supports more dynamic RTTI..? it seems to support alot of other stuff C++ is lacking...

• 9
• 23
• 10
• 19