我想将成员指针设为 shared_ptr,但我不确定 shared_ptr 本身在包含类被销毁后是否还活着。我测试了下面的代码,但我不确定它在运行时是否正确?
using std::cout;
using std::cin;
using std::endl;
class Widget
{
public:
Widget()
{
cout<<__FUNCTION__<<"()"<<endl;
}
~Widget()
{
cout<<__FUNCTION__<<"()"<<endl;
}
void display()
{
cout<<"the smart pointers are really smart"<<endl;
}
private:
};
class Window
{
public:
Window()
:widget_(new Widget())
{
}
Widget* widget()
{
return widget_.get();
}
private:
std::shared_ptr<Widget> widget_;
};
int main()
{
Widget* outer = nullptr;
{
Window wind;
outer = wind.widget();
}
outer->display();
cout<<"enter"<<endl;
cin.get();
return 0;
}
`