We use shared pointers for memory management but I still have some questions on what is the best use.
Let's say I have a class, Frame, that has a shared_ptr member Camera. The camera is shared among many frames. I have some questions about best practices:
class Frame {
public:
// Q1
Frame(std::shared_ptr<Camera> camera) : camera_(camera) {}
// Q2
std::shared_ptr<Camera> getCamera() const{ return camera_; }
// Q3
void setCamera( std::shared_ptr<Camera> camera ) { camera_ = camera; }
private:
std::shared_ptr<Camera> camera_;
};
Questions:
Q1. What should the constructor take as an argument: std::shared_ptr<Camera>, or const std::shared_ptr<Camera>&, or something else?
Q2. What should getCamera() return, std::shared_ptr<Camera>, or const std::shared_ptr<Camera>&, or const std::shared_ptr<const Camera>&, or something else? Or should we have a const and a non-cost version. And should we name the non-const version getCameraMutable()?
Q3. What should setCamera() take as an argument: std::shared_ptr<Camera>, or const std::shared_ptr<Camera>&, or something else?