I recently started trying to enforce const correctness in my code. In a function definition, I feed a constant pointer to a constant object of the class LorentzM
:
void AnalysisObject::SetOwnedPointer(const int maptotree_In, const LorentzM* const momentum_In){
owned_pp4_original.reset(momentum_In);
maptotree=maptotree_In;
}
where owned_pp4_original
is
shared_ptr<LorentzM> owned_pp4_original;
I do this, because this function, SetOwnedPointer, should never change the LorentzM* momentum_In
nor should it change the object that it points to, so constant pointer to constant object it is.
However, a shared_ptr is created to the object where momentum_In points to, and I do want to use this shared_ptr to change the object later on:
void ChangeLorentzM(const double px, const double py, const double pz, const double E){
owned_pp4_original->SetPxPyPzE(px,py,pz,E); //THIS CHANGES THE OBJECT
}
So, on one hand, to be able to do:
owned_pp4_original.reset(momentum_In);
owned_pp4_original
should be a shared_ptr<const LorentzM>
but then, I wouldn't be able to change the object through it.
What's wrong in this picture?
thanks a lot.