I was getting confused with the usage of "new" in case of shared_ptr.
(1)shared_ptr<int>sharedPointer(new int (7));
This one works fine.
(2) shared_ptr<int>sharedPointer;
sharedPointer = new int (7);
As of my current understanding in both the scenarios new is returning a pointer of int type .
In the 2nd Line ,its throwing an error of no match for operator= (operand types are std::shared_ptr<int>' and int * );
Since "new" in this case is returning an int * why this error when assignment operator comes to picture ?
Can anybody explain ,the difference in both the cases ?
Case (2) shared_ptr<int>sharedPointer; sharedPointer = new int (7);
Default construction followed by assignment. ergo, looks for an overloaded assignment operator. http://en.cppreference.com/w/cpp/memory/shared_ptr/operator%3D
Error, no overloaded assignment operator which can accept a raw pointer as the operand on the rhs.
Note that the constructor that was used in case (1) is not a converting constructor;
Converting constructor: http://en.cppreference.com/w/cpp/language/converting_constructor
(So can't implicitly convert the raw pointer to a shared pointer and then assign the result of the conversion.)