I have shared_ptr variable in my class object (ObjA). There is a requirement where this object is to be stored as (void*) entity of another Class' object (ObjB).
My doubt is, what will be the behavior of shared_ptr (will associated heap memory be freed?, what will happen to its reference count?)-
-
when ObjA is converted to void*
-
when void* entity of ObjB is cast back to (ClassA *)
Simplified Code:
Class AA{
shared_ptr<int> aptr;
public:
AA(){
aptr = make_shared<int>(100);
}
shared_ptr<int> get_aptr(){
return aptr;
}
};
Class BB{
void *smpl;
public:
void setter(void* p){
smpl = p;
}
void* getter(){
return smpl;
}
};
int main(){
AA *objA = new AA();
BB *objB = new BB();
objB->setter(objA);
//status of allocated int in class AA here?
//some code later
AA *objA_2 = (AA*)objB->getter();
//status of allocated int in class AA here?
shared_ptr<int> p2 = objA_2->get_aptr();
//is this allowed
}
Aucun commentaire:
Enregistrer un commentaire