Even though my code compiles fine, this is something that has been bugging me, and I wasn't able to find an answer on stackoverflow. The following generic constructor is one way to pass a shared_ptr to a class instance in the constructor.
MyClass {
MyClass(const std::shared_ptr<const T>& pt);
std::shared_ptr<const T> pt_; //EDITED: Removed & typo
};
MyClass::MyClass(const std::shared_ptr<const T>& pt)
: pt_(pt)
{ }
This compiles fine. My question is the following: In my understanding, declaring a parameter const like this:
void myfunc(const T& t)
promises not to change t. However, by copying the shared_ptr pt to pt_, am I not effectively increasing the use count of the shared_ptr pt, thereby violating the supposed const-ness?
This might be a fundamental misunderstanding of shared_ptrs on my side?
(For anybody reading this looking to implement it, note that this might be a better implementation)