1.头文件:
#include<memory>
2.要领:
注意我们访问auto_ptr的成员函数时用的是“.”
3. auto prt
?c++调用getxxx()方法返回引用,是否破坏了安全性
#include <iostream>
#include <memory>
using namespace std;
class Test
{
public:
Test(string s){
str = s;
cout << "test create\n";
}
~Test(){
cout << "Test delete:" << str << endl;
}
string& getStr(){
return str;
}
void setStr(string s){
str = s;
}
void print(){
cout << str << endl;
}
private:
string str;
};
int main(int argc, char *argv[])
{
auto_ptr<Test> ptest(new Test("123"));
ptest->setStr("hello");
ptest->print();
ptest.get()->print();
//return a reference is not safe
ptest->getStr()+="world!";
(*ptest).print();
ptest.reset(new Test("123"));
ptest->print();
return 0;
}
~