推荐B站视频:
4.shared_ptr计数指针_哔哩哔哩_bilibilihttps://www.bilibili.com/video/BV18B4y187uL?p=4&vd_source=a934d7fc6f47698a29dac90a922ba5a3
5.shared_ptr与函数_哔哩哔哩_bilibilihttps://www.bilibili.com/video/BV18B4y187uL?p=5&vd_source=a934d7fc6f47698a29dac90a922ba5a3
一、计数指针:shared_ptr
- shared_ptr 计数指针又称为共享指针
- 与unique_ptr不同的是它是可以共享数据的
二、shared_ptr
- shared_ptr创建了一个计数器与类对象所指的内存相关联
- Copy则计数器加一,销毁则计数器减一
- api为use_count()
三、通过例子来
- 常量类型的shared_ptr
- 自定义类型的shared_ptr
(1)常量类型的shared_ptr
#include <iostream>
#include <memory>
#include "cat.h"
using namespace std;
int main(int argc,char* argv[]) {
// 常量类型
std::shared_ptr<int> i_p_1 = make_shared<int>(10);
// std::shared_ptr<int> i_p_1 = make_shared<int>(new int(10));
cout<<"value : "<<*i_p_1<<endl;
cout<<"use count : "<<i_p_1.use_count()<<endl; // 1
// copy
std::shared_ptr<int> i_p_2 = i_p_1;
cout<<"use count : "<<i_p_1.use_count()<<endl; // 2
cout<<"use count : "<<i_p_2.use_count()<<endl; // 2
*i_p_2 = 20; // 修改值,两个指针指向同一内存,值都改变。
cout<<"value : "<<*i_p_1<<endl; // 20
cout<<"value : "<<*i_p_2<<endl; // 20
std::shared_ptr<int> i_p_3 = i_p_1; // 第三个指针指向同一内存
i_p_1 = nullptr;
cout<<"use count : "<<i_p_1.use_count()<<endl; // 0
cout<<"use count : "<<i_p_2.use_count()<<endl; // 2
cout<<"use count : "<<i_p_3.use_count()<<endl; // 2
cout<<"over~"<<endl;
return 0;
}
运行结果:
PS D:\Work\C++UserLesson\cppenv\bin\Debug> ."D:/Work/C++UserLesson/cppenv/bin/Debug/app.exe"
value : 10
use count : 1
use count : 2
use count : 2
value : 20
value : 20
use count : 0
use count : 2
use count : 2
over~
PS D:\Work\C++UserLesson\cppenv\bin\Debug>
(2)自定义类型的shared_ptr
#include <iostream>
#include <memory>
#include "cat.h"
using namespace std;
int main(int argc,char* argv[]) {
// 自定义类型
std::shared_ptr<Cat> c_p_1 = make_shared<Cat>();
cout<<"use count : "<<c_p_1.use_count()<<endl; // 1
std::shared_ptr<Cat> c_p_2 = c_p_1; // 第二个指针指向同一内存
std::shared_ptr<Cat> c_p_3 = c_p_1; // 第三个指针指向同一内存
cout<<"use count : "<<c_p_1.use_count()<<endl; // 3
cout<<"use count : "<<c_p_2.use_count()<<endl; // 3
cout<<"use count : "<<c_p_3.use_count()<<endl; // 3
c_p_1.reset();
cout<<"use count : "<<c_p_1.use_count()<<endl; // 0
cout<<"use count : "<<c_p_2.use_count()<<endl; // 2
cout<<"use count : "<<c_p_3.use_count()<<endl; // 2
cout<<"over~"<<endl;
return 0;
}
执行结果:
PS D:\Work\C++UserLesson\cppenv\bin\Debug> ."D:/Work/C++UserLesson/cppenv/bin/Debug/app.exe"
use count : 1
use count : 3
use count : 3
use count : 3
use count : 0
use count : 2
use count : 2
over~
Destructor of Cat
PS D:\Work\C++UserLesson\cppenv\bin\Debug>
四、shared_ptr与函数
- shared_ptr passed by value
- copy
- 函数内部计数器加一
- shared_ptr passed by ref
- const 表示不可改变指向
- returning by value
(1)shared_ptr passed by value
#include <iostream>
#include <memory>
#include "cat.h"
using namespace std;
void cat_by_value(std::shared_ptr<Cat> cat) {
cout<<cat->get_name()<<endl;
cat->set_cat_name("JiaFeiMao");
cat->catInfo();
cout<<"func use count : "<<cat.use_count()<<endl;//2
}
int main(int argc,char* argv[]) {
std::shared_ptr<Cat> c1 = make_shared<Cat>("TomCat");
cat_by_value(c1);
c1->catInfo();
cout<<"c1 use count : "<<c1.use_count()<<endl; // 1
cout<<"over~"<<endl;
return 0;
}
PS D:\Work\C++UserLesson\cppenv\bin\Debug> ."D:/Work/C++UserLesson/cppenv/bin/Debug/app.exe"
Constructor of Cat : TomCat
TomCat
cat info name : JiaFeiMao
func use count : 2
cat info name : JiaFeiMao
c1 use count : 1
over~
Destructor of Cat
PS D:\Work\C++UserLesson\cppenv\bin\Debug>
(2)shared_ptr passed by ref
#include <iostream>
#include <memory>
#include "cat.h"
using namespace std;
void cat_by_ref(std::shared_ptr<Cat> &cat) {
cout<<cat->get_name()<<endl;
cat.reset(new Cat());
cat->catInfo();
cout<<"func use count : "<<cat.use_count()<<endl;//1
}
int main(int argc,char* argv[]) {
std::shared_ptr<Cat> c1 = make_shared<Cat>("TomCat");
cat_by_ref(c1);
c1->catInfo();
cout<<"over~"<<endl;
return 0;
}
PS D:\Work\C++UserLesson\cppenv\bin\Debug> ."D:/Work/C++UserLesson/cppenv/bin/Debug/app.exe"
Constructor of Cat : TomCat
TomCat
Destructor of Cat
cat info name : Mimi
func use count : 1
cat info name : Mimi
over~
Destructor of Cat
PS D:\Work\C++UserLesson\cppenv\bin\Debug>
注意:如果加上const就不能使用reset
#include <iostream>
#include <memory>
#include "cat.h"
using namespace std;
void cat_by_ref(const std::shared_ptr<Cat> &cat) {
cout<<cat->get_name()<<endl;
// cat.reset(new Cat()); error
cat->catInfo();
cout<<"func use count : "<<cat.use_count()<<endl;//1
}
int main(int argc,char* argv[]) {
/*
// 自定义类型
std::shared_ptr<Cat> c_p_1 = make_shared<Cat>();
cout<<"use count : "<<c_p_1.use_count()<<endl; // 1
std::shared_ptr<Cat> c_p_2 = c_p_1; // 第二个指针指向同一内存
std::shared_ptr<Cat> c_p_3 = c_p_1; // 第三个指针指向同一内存
cout<<"use count : "<<c_p_1.use_count()<<endl; // 3
cout<<"use count : "<<c_p_2.use_count()<<endl; // 3
cout<<"use count : "<<c_p_3.use_count()<<endl; // 3
c_p_1.reset();
cout<<"use count : "<<c_p_1.use_count()<<endl; // 0
cout<<"use count : "<<c_p_2.use_count()<<endl; // 2
cout<<"use count : "<<c_p_3.use_count()<<endl; // 2
*/
std::shared_ptr<Cat> c1 = make_shared<Cat>("TomCat");
cat_by_ref(c1);
c1->catInfo();
cout<<"over~"<<endl;
return 0;
}
PS D:\Work\C++UserLesson\cppenv\bin\Debug> ."D:/Work/C++UserLesson/cppenv/bin/Debug/app.exe"
Constructor of Cat : TomCat
TomCat
cat info name : TomCat
func use count : 1
cat info name : TomCat
over~
Destructor of Cat
PS D:\Work\C++UserLesson\cppenv\bin\Debug>
(3)returning by value
#include <iostream>
#include <memory>
#include "cat.h"
using namespace std;
std::shared_ptr<Cat> get_shared_ptr() {
std::shared_ptr<Cat> cat_p = std::make_shared<Cat>("lanmao");
return cat_p;
}
int main(int argc,char* argv[]) {
std::shared_ptr<Cat> c_p = get_shared_ptr();
c_p->catInfo();
get_shared_ptr()->catInfo();
cout<<"over~"<<endl;
return 0;
}
执行结果:
PS D:\Work\C++UserLesson\cppenv\bin\Debug> ."D:/Work/C++UserLesson/cppenv/bin/Debug/app.exe"
Constructor of Cat : lanmao
cat info name : lanmao
Constructor of Cat : lanmao
cat info name : lanmao
Destructor of Cat
over~
Destructor of Cat
PS D:\Work\C++UserLesson\cppenv\bin\Debug>