no downside to smart pointers - don't lose "this" keyword

Started by
3 comments, last by yoshscout 16 years, 6 months ago
[Edit] i was wrong one downside is that you must pass the smart pointer always. in essence, this takes away the "this" keyword. example where i learned this(psuedocode) class Resource has std::list of weak ptrs to Instances class Instance has smart pointer to Resource has a member function to change the Resource if i want to assign a resource to Instance, i can not make instance intelligently pass itself to Resource(and even more important resource pass itself to other subsystems ie loading queue) without sending it the smart pointer of itself it is possible by doing AssignResource(ResourcePtr pRes, InstancePtr pThis); but i have never had to pass an object a pointer to itself so this is really ... fairly ugly i think by removing "this" u really lose a powerful keyword [Edited by - yoshscout on September 29, 2007 3:58:11 PM]
Advertisement
That assumes that having objects pass themselves around is a good idea in the first place.
It's perfectly possible to use the this keyboard. If you're using Boost's smart pointers, use the shared_from_this idiom. It's in the documentation.
well it seems to me like a pretty good design for a resources to be queued to the background loading thread as objects are created that require the resource(it is more container like)

additionally when no objects are using a resource you have a way of catching that in the objects destructor(again more container like)

the only ways to solve this is to keep a weak ptr filled in by the smart pointer the object was created by, or to change the design to have a 3rd class baby sit all of this(constructs the object - passes it to resource - queues it to resource loader if needed - returns object)
Quote:Original post by jpetrie
It's perfectly possible to use the this keyboard. If you're using Boost's smart pointers, use the shared_from_this idiom. It's in the documentation.


awesome

This topic is closed to new replies.

Advertisement