当前位置:网站首页>std::unique_ The use of PTR as a formal parameter
std::unique_ The use of PTR as a formal parameter
2022-07-18 03:39:00 【~What's going on~】
#include <iostream>
#include <memory>
class Test
{
};
void dowork(std::unique_ptr<Test> p){
std::cout << "Hello, dowork!" << std::endl;
}
int main() {
std::cout << "Hello, World!" << std::endl;
std::unique_ptr<Test> p(new Test());
dowork(p);
return 0;
} Report errors :main.cpp: In function ‘int main()’:
main.cpp:18:13: error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = Test; _Dp = std::default_delete<Test>]’
18 | dowork(p);
| ^
In file included from /usr/include/c++/9/memory:80,
from main.cpp:2:
/usr/include/c++/9/bits/unique_ptr.h:414:7: note: declared here
414 | unique_ptr(const unique_ptr&) = delete;
| ^~~~~~~~~~
main.cpp:10:35: note: initializing argument 1 of ‘void dowork(std::unique_ptr<Test>)’
10 | void dowork(std::unique_ptr<Test> p){
problem :unique_ptr The memory area pointed to can only consist of one unique_ptr Object to specify . hold p As an incoming parameter , You will find two unique_ptr At the same time, it points to a memory area , The actual parameter p And formal parameters .
1 If this is the case main Function std::unique_ptr<Test> p Ownership passes to function dowork
modify :
#include <iostream>
#include <memory>
class Test
{
};
void dowork(std::unique_ptr<Test> p){
std::cout << "Hello, dowork!" << std::endl;
}
int main() {
std::cout << "Hello, World!" << std::endl;
std::unique_ptr<Test> p(new Test());
dowork(std::move(p));
std::cout << "p in main" << p.get() << std::endl;
return 0;
}
2 dowork Is only for use p, Then change the parameter type of the function to the form of reference , In this way, the parameter will not be copied in the process of parameter transfer ,main Medium p and dowork Medium p Represents the same object .
#include <iostream>
#include <memory>
class Test
{
};
void dowork(const std::unique_ptr<Test> &p){
std::cout << "Hello, dowork! => " << p.get() << std::endl;
}
int main() {
std::cout << "Hello, World!" << std::endl;
std::unique_ptr<Test> p(new Test());
dowork(std::move(p));
std::cout << "p in main =>" << p.get() << std::endl;
return 0;
}
3 Use std::unique_ptr Don't use raw pointers after managing them
#include <iostream>
#include <memory>
class Test
{
public:
Test(){
std::cout << "Hello, Test!" << std::endl;
}
~Test(){
std::cout << "Hello, ~Test!" << std::endl;
}
int test(){
return num;
}
private:
int num = 10;
};
void dowork(std::unique_ptr<Test> p){
std::cout << "Hello, dowork! => " << p.get() << std::endl;
}
int main() {
std::cout << "Hello, World!" << std::endl;
Test* p = new Test();
std::unique_ptr<Test> ptr(p);
std::cout << "p in main =>" << p << " p.test:" << p->test() << std::endl;
ptr.reset();
std::cout << "p in main =>" << p << " p.test:" << p->test() << std::endl;
while(1);
return 0;
}
stay ptr call reset after ,p The reference count is 0, Deconstruction , stay ubuntu9.4.0

Next p You can still call , But it's junk value
边栏推荐
- [detailed explanation] paging method: use of page helper
- 活动预告|Apache Doris x Apache SeaTunnel 联合 Meetup 开启报名!
- H5 implementation generates urlscheme and jumps from H5 to applet
- 中国人力资源数字化生态图谱-灵活用工市场
- Use USBASP to write programs to Arduino uno through ICSP mode
- Thesis learning (I) -- MWP bert: numerical augmented pre training for math wordproblem solving
- DEVKIT-mpc5744p配置rtos
- File parsing_ Excel file parsing
- The latest research of Zhu Songchun's team: robots can "confide" with humans! Also said that the next step is to create "Ai white"
- 聊聊异步编程的 7 种实现方式
猜你喜欢

授人以渔-在 SAP MM 物料显示界面上看到一个字段,如何查找哪张数据库表的哪个字段进行的存储

两年CRUD,普通二本毕业,挑战三个月面试阿里,成功拿下offer定级P7!年薪50w

Accenture's 22 year technology outlook report: digital transformation will usher in the next decade

难道双非本科就一定进不了大厂?阿里技术四面+交叉面+HR面,成功拿到offer!!

The latest research of Zhu Songchun's team: robots can "confide" with humans! Also said that the next step is to create "Ai white"

Tikv & tiflash accelerates complex business queries

Infrared remote control atmosphere lamp touch chip-dlt8sa15a-jericho

水电站设备也能远程运维

Offstage in shutter

Play about the workplace: Senior HR tells you what characteristics strong people in the workplace have
随机推荐
The difference between B tree and b+ tree
【visdom绘图】深度学习中Visdom绘图的总结
Redis connection pool
std::unique_ptr作为形参时的使用问题
JS to realize the function of electronic signature
二叉树上的相等子树
Where is the IBD file of MySQL in win11 virtual machine
Want to be an elite developer? Please force yourself to form these 10 habits
leetcode:300. Longest increasing subsequence [LIS board + greedy dichotomy + nlogn]
Google Earth Engine(GEE)——S2影像异常值
普通二本毕业五年,备战两个月,四面阿里巴巴定级P7
mysql(三)路由器、MHA高可用
torch.nn.CTCLoss()的使用
MySQL (III) router, MHA high availability
聊聊异步编程的 7 种实现方式
[development tutorial 17] AI voice face recognition (Conference recorder / face punch) -ai face registration, authentication and recognition
西山居如何用 ONES 打造游戏工业流水线?|ONES 行业实践
【OpenCV 例程200篇】230. 特征描述之 LBP 统计直方图
Sony's metauniverse layout
Musk suspends twitter acquisition, with a breakup fee of $1billion