Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

const correctness and shared_ptr, a matter of design?

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.

like image 345
elelias Avatar asked Dec 14 '12 14:12

elelias


People also ask

What does const Shared_ptr mean?

As a return value, the const in boost::shared_ptr<Bar> const means that you cannot call a non-const function on the returned temporary; if it were for a real pointer (e.g. Bar* const ), it would be completely ignored.

Why is const correctness important?

The benefit of const correctness is that it prevents you from inadvertently modifying something you didn't expect would be modified.

Can unique_ ptr be const?

const std::unique_ptr<T> It means that we cannot reset the pointer, we cannot change what it points to. At the same time, the value it points to can be modified. It's worth noting that if you have a const unique_ptr , you're very limited in what you can do. You cannot even return it as it requires moving away from it.

What is const& in c++?

The const keyword specifies that a variable's value is constant and tells the compiler to prevent the programmer from modifying it.


1 Answers

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.

That's not reason enough. The function may not change the object directly, but it is granting another object (the shared_ptr) rights to modify the object. It can't grant rights that it doesn't have itself. So change it to a pointer to a non-const object.

It's the same thing, for example, if you have a class that has a non-const reference member. You need to pass a non-const object to its constructor, even if the constructor does not itself modify the object. The constructor needs to grant the object's reference member rights to modify the object, and it can't do that if it does not itself have those rights.

like image 133
Benjamin Lindley Avatar answered Sep 30 '22 08:09

Benjamin Lindley