C++ 智能指针 — 探索智能指针变量在类中销毁的顺序(学习笔记二)

各位路过的大神,要看懂我这篇文章,需要看一下我的第一章笔记:

        https://blog.csdn.net/acdfg123/article/details/118226287?spm=1001.2014.3001.5502

我这一章是基于前一章的序章,后续的笔记都是由第一章的最基础工程来进行实验

1.创建新类 TestB(直接复制TestA的内容),内容如下:

class TestB
{
public:
	TestB(const std::string inName) :_name(inName), _val(-1) {}
	TestB(const std::string inName, int inVal) :_name(inName), _val(inVal) {}
	~TestB()
	{
		std::cout << "~TestB: " << _name << " : " << _val << std::endl;
	}
private:

	std::string _name;
	int _val;
};

2. 修改 TestA 的内容,修改后如下

class TestA
{
public:
	TestA(const std::string inName)
		:_name(inName),
		_val(-1),
		_testB(new TestB(inName + "->_testB",_val))
		{
			
		}
	TestA(const std::string inName,int inVal)
		:_name(inName),
		_val(inVal),
		_testB(new TestB(inName + "->_testB", _val))
		{}
	~TestA()
    {
        std::cout<<"~TestA: create class name: " <<_name<< ": " <<_val << std::endl;
    }
private:

	std::string _name;
	int _val;
	CSharedPtr<TestB> _testB;
};

其它代码保持不变,运行输出结果如下:

由结果看出:在 _C 被销毁后,其里面的智能指针变量 _testB 其后才被销毁,_A也是如此

由此得出销毁顺序:类 —>类里的智能指针变量

3.再次修改TestB代码,在TestB中添加一个智能指针变量,并修改构造,修改结果如下

TestB.h 文件

#pragma once
#include <iostream>
#include <cstring>
#include "Shared.h"

class TestA;

class TestB
{
public:
	TestB(const std::string inName);
	TestB(const std::string inName, int inVal);
	~TestB()
	{
		std::cout << "~TestB: " << _name << " : " << _val << std::endl;
	}
private:

	std::string _name;
	int _val;
	CSharedPtr<class TestA> _testA;
};

TestB.cpp文件

#include "TestB.h"
#include "TestA.h"

TestB::TestB(const std::string inName)
	:_name(inName), _val(-1), _testA(new TestA(inName + "->_testA", _val,false))
{}

TestB::TestB(const std::string inName, int inVal)
	: _name(inName), _val(inVal), _testA(new TestA(inName + "->_testA", _val,false))
{}

同样的TestA的 构造函数实现,全部移到  cpp 里。但是为了避免循环构造,导致崩溃,在TestA里新增了一个构造函数 TestA(const std::string inName,int inVal,bool bCreateTestB = false);后面创建TestA都使用该函数

.h文件修改后的

#pragma once

#include <iostream>
#include <cstring>
#include "Shared.h"

class TestB;

class TestA
{
public:
	TestA(const std::string inName);
	TestA(const std::string inName,int inVal);
	TestA(const std::string inName,int inVal,bool bCreateTestB = false);
	~TestA();
private:

	std::string _name;
	int _val;
	CSharedPtr<TestB> _testB;
};

cpp文件修改后如下:

#include "TestA.h"
#include "TestB.h"

TestA::TestA(const std::string inName)
	:_name(inName),
	_val(-1),
	_testB(nullptr)
{}

TestA::TestA(const std::string inName, int inVal)
	:_name(inName),
	_val(inVal),
	_testB(nullptr)
{}

TestA::TestA(const std::string inName, int inVal, bool bCreateTestB /*= false*/)
	: _name(inName),
	_val(inVal),
	_testB(bCreateTestB? new TestB(inName + "->_testB", _val) : nullptr)
{}

main函数也做出如下修改

#include "Shared.h"
#include "TestA.h"
#include <iostream>

int main()
{
	{
		CSharedPtr<TestA> _A(new TestA("_A",5,true));
		CSharedPtr<TestA> _B(_A);
		CSharedPtr<TestA> _C(new TestA("_C",10,false));
		_C=_B;
		std::cout << "A:"<< _A.GetCount() << std::endl;
		std::cout << "B:"<< _B.GetCount() << std::endl;
		std::cout << "C:"<< _C.GetCount() << std::endl;

	}
	return 0;
}

然后运行结果

结果可以看出类的销毁顺序为:类 —>类里的智能指针变量类1—>类1里的智能指针变量类2

从代码实验结果来看,智能指针的销毁顺序是一个

   类 —>类里的智能指针变量类1—>类1里的智能指针变量类2—>类2里的智能指针变量类3—>......

这样的一个过程,它会从最开始的那个类,一直向里面的智能指针变量,逐级进行删除销毁

猜你喜欢

转载自blog.csdn.net/acdfg123/article/details/118248460