Simple example of c++ smart pointer

the code

#include<iostream>
using namespace std;
#include<memory> // 头文件

class TestClass
{
    
    
private:
	int Value;

public:
	TestClass(int value) :Value(value) {
    
    
		cout << "构造函数调用" << endl;
	}
	~TestClass() {
    
    
		cout << "析构函数调用" << endl;
	}

	void hello()
	{
    
    
		cout << "hello, smart ptr!" << endl;
	}
};

int main()
{
    
    
	shared_ptr<TestClass> pTest(new TestClass(8));
	pTest->hello();

	return 0;
}

result

insert image description here

Guess you like

Origin blog.csdn.net/sdhdsf132452/article/details/131733231