c++智能针对测试示例unique_ptr&shared_ptr

#include <iostream>
#include <memory>
#include <string>
using namespace std;

class Test {
public:
	Test() { cout << "Test的构造函数..." << endl; }
	~Test() { cout << "Test的析构函数..." << endl; }

	int getDebug() { return this->debug; }

private:
	int debug = 20;
};


class Person {
public:
	Person(int v) {
		this->no = v;
		cout << "构造函数 \t no = " << this->no << endl;
	}

	~Person() {
		cout << "析构函数 \t no = " << this->no << endl;
	}

private:
	int no;
};

// 仿函数,内存删除
class DestructPerson {
public:
	void operator() (Person* pt) {
		cout << "DestructPerson..." << endl;
		delete pt;
	}
};


int main()
{
	cout << "Hello CMake." << endl;

	//Test* test = new Test; //不会调用析构函数

	//auto_ptr<Test> test(new Test);

	unique_ptr<string> p1(new string("I'm Li Ming!"));
	unique_ptr<string> p2(new string("I'm age 22."));

	
	string str1 = "I'm age 33.";
	unique_ptr<string> p3 = std::make_unique<string>(str1);//ok,创建一个
	//unique_ptr<string> p4 = std::make_unique<string>( new string("I'm age 44."));//make_ 构造函数参数不能用new
	unique_ptr<string> p4 = std::make_unique<string>( string("I'm age 44."));

	cout << "p1:" << p1.get() << endl;
	cout << "p2:" << p2.get() << endl;
	cout << "p3:" << p3.get() << endl;
	cout << "p4:" << p4.get() <<"," << p4.get()->c_str() << endl;

	//p1 = p2;					// 禁止左值赋值
	//unique_ptr<string> p3(p2);	// 禁止左值赋值构造


	p1 = std::move(p2);	// 使用move把左值转成右值就可以赋值了,效果和auto_ptr赋值一样

	cout << "p1 = p2 赋值后:" << endl;
	cout << "p1:" << p1.get() << endl;
	cout << "p2:" << p2.get() << endl;


	p1 = std::move(p3);	// 使用move把左值转成右值就可以赋值了,效果和auto_ptr赋值一样

	cout << "p3 = p1 赋值后:" << endl;
	cout << "p1:" << p1.get() << endl;
	cout << "p3:" << p3.get() << endl;


	//shared_ptr
	cout << "shared_ptr==== " << endl;
	shared_ptr<Person> sp1;

	shared_ptr<Person> sp2(new Person(2));

	// 获取智能指针管控的共享指针的数量	use_count():引用计数
	cout << sp1.get() << " sp1	use_count() = " << sp1.use_count() << endl;
	cout << sp2.get() << " sp2	use_count() = " << sp2.use_count() << endl << endl;

	// 共享
	sp1 = sp2;

	cout << sp1.get() << " sp1	use_count() = "  << sp1.use_count() << endl;
	cout << sp2.get() << " sp2	use_count() = "  << sp2.use_count() << endl << endl;

	shared_ptr<Person> sp3(sp1);
	cout << sp1.get() << " sp1	use_count() = "  << sp1.use_count() << endl;
	cout << sp2.get() << " sp2	use_count() = "  << sp2.use_count() << endl;
	cout << sp3.get() << " sp3	use_count() = "  << sp3.use_count() << endl << endl;

	shared_ptr<Person> sp4 = make_shared<Person>(10);
	cout << sp4.get() << " sp4	use_count() = "  << sp4.use_count() << endl << endl;
	Person* person4 = sp4.get();
	cout << "person4 addr:= " << person4 << endl << endl;


	Person* person5 = new Person(4);
	cout << "person5 addr:= " << person5 << endl;
	shared_ptr<Person> sp5(person5); //指针赋值给智能指针
	cout << "sp5 addr:= " << sp5.get() << endl << endl; //智能指针获取指针

	//shared_ptr<Person> sp6(nullptr);//ok
	shared_ptr<Person> sp6;	
	cout << sp6.get() << " sp6	use_count() = " << sp6.use_count() << endl;
	if (sp6 ==nullptr)
	{
		cout << "sp6 is null" << endl;
	}
	else
		cout << sp6.get() << " sp6 is not null" << endl;

	sp6 = make_shared<Person>(101);
	cout << sp6.get() << " sp6	use_count() = " << sp6.use_count() << endl;

	return 0;
}

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值