小编ses*_*zed的帖子

抛出后为什么shared_ptr为NULL

我在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)

c++ gcc throw shared-ptr

8
推荐指数
0
解决办法
69
查看次数

标签 统计

c++ ×1

gcc ×1

shared-ptr ×1

throw ×1