I have a “handle” class IHandle derived from TObject, and that class is a kind of shared_ptr with -> operator overloaded, and the pointer pointing to pointee is inside IHandle.
Now I have another function GetHandle(), that returns the IHandle of some object, let’s say IHandle<CLASSA>. I try to save it into TFile by using:
GetHandle()->Write();
will this cause crash or memory issue like dangling pointer? the Object of IHandle will only valid until the end of this line I suppose.
with -> operator overloaded, and the pointer pointing to pointee is inside IHandle. GetHandle()->Write();
I am not sure from your description what object is temporary. operator-> is putting on the stack (per se) (and/or using a temporary for) the value of the pointer to the pointee (so the pointee, the object is not temporary).
Either way calling TObject::Write on a temporary is in general just fine since by C++ rule that temporary lifetime will extent to the end of the statement.
Sorry for the unclear description. IHandle is a class contains a pointer value pointing to some object, and also a count number (just like shared_ptr)
You mean as long as the object is valid before the end of the statement (“;” sign) it can be successfully written?
If it’s a IHandle class with -> overloaded, and called handleToClassA->Write() where handleToClassA is the type of IHandle<ClassA>, will the IHandle object itself (with the pointer value 0xADDRESS) been written in TFile or will the pointee ClassA been written?
By definition, in that scenario, Write never sees the handle since it is called on the pointee’s address (that’s the ‘magic’ of the operator-> overload.
I.e. the pointee ClassA will be written?