C++智能指针(二)

作者:Rem ㅤ | 发布时间:

 unique_ptr

unique_ptr在程序编译时就检测是否存在指针所有权改变的情况,若存在则编译报错。

#include <iostream>
#include <memory>

using namespace std;

class Test_3
{
public:
    Test_3() : data(0) { cout << "Test_3 constructor called" << endl; } // constructor for Test_3 class
    ~Test_3() { cout << "Test_3 destructor called" << endl; }           // destructor for Test_3 class

private:
    int data;
};

void Fun()
{
    unique_ptr<Test_3> pTest1(new Test_3);
    unique_ptr<Test_3> pTest2;
    pTest2 = pTest1;
}
int main(int argc, char **argv)
{
    Fun();
    system("pause");
    return 0;
}

报错: 20230411140624

标签:c/c++