If you have a class that has a private shared_ptr defined in the header like so:
class myClass {
public:
...
private:
std::shared_ptr<int> testint;
void doSomething();
};
Later in doSomething (in myClass.cpp) what is the best way to initialize it?
void myClass::doSomething() {
int i = getNumberFromFile("id.conf");
testint(new int(i)); // does not work
testint.reset(new int(i)); //seems to work, but is this the right way?
testint = std::make_shared<int>(i); // or is this better
}
Deduplicator: My question is what is the proper way to initialize a shared_ptr in a class function when the definition for the shared_ptr is in a the header file. Not what is the difference between using shared_ptr initialization and make_shared.