智能指针不可以解引用赋值吗?
#include <iostream>#include <stdio.h>
#include <memory>
#include <string>
using namespace std;
int main( )
{
shared_ptr<string> p1 ;
string str = "abc" ;
if ( !p1 ) {
*p1 = str ;
}
cout<<*p1<<endl;
return 0 ;
}
#include <iostream> #include <memory> #include <string> using namespace std; int main( void ) { shared_ptr<string> p = std::make_shared<string>(); *p = "abc"; cout << *p << endl; return 0; }
#include <iostream> #include <stdio.h> #include <malloc.h> #include <memory> #include <string> using namespace std ; int main( ) { shared_ptr<string> p1 = make_shared<string>( ) ; string str = "abc"; *p1 = str; cout << (*p1) << endl; return 0; }