我在C++中有一个shared_ptr,我想把它作为例外抛出.我正在编写一个测试用例,在gcc 6.4.0中使用了相同的情况并且卡住了.
我不明白为什么在处理异常后x为NULL,即使它没有超出范围或被分配给.
#include "stdafx.h"
#include <iostream>
#include <memory>
#include <string>
using namespace std;
class S {
private:
string s;
public:
string String()
{
return s;
}
S(string a) : s(a)
{
cout << "S::S( " << a << " )" << endl;
}
~S()
{
cout << "S::~S( " << String() << " )" << endl;
}
};
int main(int argc, char* argv[])
{
shared_ptr< S > x = make_shared<S>("hello world");
cout << "Before throw" << endl;
try { …Run Code Online (Sandbox Code Playgroud)