1.unique_ptr
智能指针之unique_ptr(详解)_unique_ptr 函数参数_肥肥胖胖是太阳的博客-CSDN博客
unique_ptr 代表的是专属所有权,即由 unique_ptr 管理的内存,只能被一个对象持有。
所以,unique_ptr 不支持复制和赋值
也不建议让2个unique_ptr指向同一个对象,智能指针释放的时候,会释放2次对象,引起崩溃(会有崩溃):
int *pValue = new int(100);
unique_ptr<int> uptrValue(pValue);
unique_ptr<int> uptrValue1(pValue);
//建议 unique_ptr<int> uptrValue2(new int(100));
std::unique_ptr::release
将智能指针管理的对象释放出去,但是并不删除对象。
unique_ptr作为函数参数(
不可以使用值传递的方式进行函数参数的传递,所以可以将函数定义为
void TestFunc(std::unique_ptr<TestC>& ptrC); 使用引用传递
如果定义为void TestFunc(std::unique_ptr<TestC> ptrC),则使用move来传递参数,如下
#include <stdio.h>
#include <memory>
using namespace std;
#include <iostream>
class TestC {
public:
TestC(int tmpa, int tmpb) :a(tmpa), b(tmpb) {
std::cout << "construct TestC " << std::endl;
}
~TestC() {
std::cout << "destruct TestC " << std::endl;
}
void print() {
std::cout << "print a " << a << " b " << b << std::endl;
}
private:
int a = 10;
int b = 5;
};
void TestFunc(std::unique_ptr<TestC> ptrC) {
printf("TestFunc called \n");
ptrC->print();
}
int main(int argc, char* argv[]) {
std::unique_ptr<TestC> gPtrC(new TestC(2, 3));
//unique_ptr<TestC> uptrTest = gPtrC;
//初始化也可以写成如下这一句
//std::unique_ptr<TestC> gPtrC = std::make_unique<TestC>(2, 3);
TestFunc(std::move(gPtrC));
//执行下面这一句会崩溃,因为gPtrC已经没有控制权
gPtrC->print();
char ch;
cin >> ch;
return 0;
}
2.shared_ptr
多线程环境中使用共享指针的代价非常大,为保证线程安全需要加锁,要考虑因为 share_ptr 维护引用计数而造成的上下文切换开销。使用它们是需要一定的开销的
shared_ptr::reset()全网最详细准确的解析:
调用reset的shared_ptr 对象变为empty,与对象失去关联,与该对象关联的其他shared_ptr::use_count 引用计数减一,当与对象关联的shared_ptr引用计数为0时,析构对象
class A {
public:
A() {
cout << "A的构造" << endl;
}
~A() {
cout << "A的析构" << endl;
}
void slot_function() {
cout << "接收到了信号,并执行槽函数的逻辑 " << m_iAge << endl;
}
int m_iAge;
};
int main()
{
A *pA = new A();
shared_ptr<A> pa(pA); //不推荐,推荐 pa(new A)
int iCount = pa.use_count(); //默认引用计数为1
shared_ptr<A> pb = pa; //pa引用计数+1, pa, pb引用计数为2
iCount = pb.use_count();
shared_ptr<A> pc = pb; //pa引用计数+1, pb引用计数为2
iCount = pa.use_count(); //pa的引用为3
iCount = pb.use_count(); //pb的引用为3
iCount = pc.use_count(); //pb的引用为3
pa.reset();
iCount = pa.use_count(); //pa的引用为0,调用pa->slot_function();会崩溃了
iCount = pb.use_count(); //pb的引用为2
iCount = pc.use_count(); //pb的引用为2
pc.reset();
pb.reset(); //当A的引用计数为0的时候,会调用A的析构函数
char ch;
cin >> ch;
return 0;
}
作为参数
CDev * GetDev(shared_ptr<CDev>* sp)
{
int iCount = sp->use_count();
return sp->get();
}
CDev * GetDevEx(shared_ptr<CDev> sp)
{
int iCount = sp.use_count();
return sp.get();
}
shared_ptr<Test> sptrTest = shared_ptr<Test>(new Test());
shared_ptr<Test> sptrTest1 = sptrTest;
//shared_ptr<Test> sptrTest2 = sptrTest;
sptrTest.reset(new Test()); //原来的对象并不会立马释放,因为此时引用计数为2 -1 =1,当引用计数为1的时候释放
3. weak_ptr
它的构造不会引起指针引用计数的增加, 但是lock()返回shared_ptr对象后,会使得原来的shared_ptr引用计数加1,所以除非主动delete对象,否则对象是不会被释放的