box2d - c++ - raw pointer to shared_ptr -
i'm using box2d , may know, holds void* object can use reference when collisions occur between different entities. problem original item saved inside shared_ptr since ownership unknown , different classes (example player class) can 'equip' class (weapon).
i'm wondering if possible put pointer inside shared_ptr , refer same object original one?
this example:
std::vector<std::shared_ptr<environment>> listenvironment; listenvironment.push_back(std::make_shared(new weapon())); //takes void pointer box2d->userid = listenvironment.back().get(); //some shit happens somewhere else , collision occurs , pointer box2d's callback: environment* envptr = static_cast<environment*>(box2d->userid);
as can see envptr going cause trouble. there way refer old smart-pointer , increase reference value?
ps:
in actuality every class creates box2d body holds 'this' pointer don't have address smart-pointer either. example above kind narrowed down give hint of problem i'm facing.
best regards nilo
if environment
has std::enable_shared_from_this
<environment>
parent class then, yes. call envptr->shared_from_this()
.
Comments
Post a Comment