当前位置:网站首页>Manually write smart pointer shared_ PTR function
Manually write smart pointer shared_ PTR function
2022-04-23 04:44:00 【Vivid_ Mm】
Environmental Science CLion + MinGW
// Manually override the smart pointer
#include "CustomPtr.h"
class Student {
public:
~Student() {
cout << " Destructor Release Student" << endl;
}
};
// TODO Built in smart pointer
void action() {
Student *student1 = new Student();
Student *student2 = new Student();
// TODO Case one
// shared_ptr<Student> sharedPtr1(student1);
// shared_ptr<Student> sharedPtr2(student2);
// TODO The second case
shared_ptr<Student> sharedPtr1 (student1);
shared_ptr<Student> sharedPtr2 = sharedPtr1;
// TODO Print
cout << " Built in smart pointer sharedPtr1:" << sharedPtr1.use_count() << endl;
cout << " Built in smart pointer sharedPtr2:" << sharedPtr2.use_count() << endl;
}
void action2() {
Student *student1 = new Student();
Student *student2 = new Student();
// TODO Case one
// Ptr<Student> sharedPtr1(student1);
// Ptr<Student> sharedPtr2(student2);
// TODO The second case copy constructor
// Ptr<Student> sharedPtr1 (student1);
// Ptr<Student> sharedPtr2 = sharedPtr1;
// TODO The second case
// TODO Situation 1
// Ptr<Student> sharedPtr1 (student1); // Call the copy constructor of the handwritten smart pointer
// Ptr<Student> sharedPtr2;
// sharedPtr2 = sharedPtr1;
// TODO Situation two
Ptr<Student> sharedPtr1 (student1); // Call the of handwriting smart pointer = Operator overload function
Ptr<Student> sharedPtr2 (student2);
sharedPtr2 = sharedPtr1;
// TODO Print
cout << " Handwritten smart pointer sharedPtr1:" << sharedPtr1.use_count() << endl;
cout << " Handwritten smart pointer sharedPtr2:" << sharedPtr2.use_count() << endl;
}
int main(){
// cout << " Here is C++ Built in smart pointer ===========" << endl;
// action();
// cout << endl;
cout << " Here is Custom smart pointer ===========" << endl;
action2();
return 0;
}
#ifndef TEST_CLION_CUSTOMPTR_H
#define TEST_CLION_CUSTOMPTR_H
#pragma once
#include <iostream>
#include <memory> // Smart pointer
using namespace std;
template<typename T>
class Ptr {
private:
T *object;
int *count;
public:
Ptr() {
// Null parameter constructor , When new Execute when an empty object
count = new int(1);
object = 0;
}
Ptr(T *t) : object(t) {
// When new With a T Execute when the parameter object of
count = new int(1);
};
~Ptr() {
if (--(*count) == 0) {
if (object) {
delete object;
}
// Zeroing
delete count;
object = 0;
count = 0;
}
}
Ptr(const Ptr<T> &p) {
cout << " copy constructor " << endl;
++(*p.count);
object = p.object; // When executed, the left side is through this Get a new object On the right is the old object coming in
count = p.count;
}
Ptr<T> &operator=(const Ptr<T> &p) {
cout << "= Operator overload " << endl;
++(*p.count);
if (--(*count) == 0) {
// scene : object 2 = object 1 operation : Object first 2 To analyze At present, the object is cleared 2 All members point to , And then assign the value
if (object) {
// Otherwise, the object 2 The assignment object belongs to the field before assignment
delete object;
}
delete count;
}
object = p.object; // When executing, the left side is the object 2(this Point to ) On the right is the incoming object 1
count = p.count;
return *this; // Operator overloaded return
}
// Returns the number of references to the object
int use_count() {
return *this->count;
}
};
#endif //TEST_CLION_CUSTOMPTR_H
版权声明
本文为[Vivid_ Mm]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220558084199.html
边栏推荐
- What is the thirty-six plan
- Com alibaba. Common methods of fastjson
- Eight misunderstandings that should be avoided in data visualization
- getprop 属性
- Solutions to the failure of sqoop connection to MySQL
- 简单的拖拽物体到物品栏
- thymeleaf th:value 为null时报错问题
- leetcode007--判断字符串中的括号是否匹配
- C language: Advanced pointer
- Unity攝像頭跟隨鼠標旋轉
猜你喜欢
第四章 --- 了解标准设备文件、过滤器和管道
Supplement: Annotation
Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.1.15.
383. Ransom letter
zynq平台交叉编译器的安装
Detailed explanation of life cycle component of jetpack
Key points of AWS eks deployment and differences between console and eksctl creation
Unity RawImage背景无缝连接移动
Unity rawimage background seamlessly connected mobile
基于英飞凌MCU GTM模块的无刷电机驱动方案开源啦
随机推荐
C language: Advanced pointer
IEEE Transactions on systems, man, and Cybernetics: Notes for systems (TSMC)
A lifetime of needs, team collaboration can play this way on cloud nailing applet
Supplement: Annotation
Leetcode004 -- Roman numeral to integer
Recommended scheme of national manufactured electronic components
Phishing for NFT
Summary of MySQL de duplication methods
2020 is coming to an end, special and unforgettable.
拼了!两所A级大学,六所B级大学,纷纷撤销软件工程硕士点!
Mysql, binlog log query
重剑无锋,大巧不工
Unity camera rotation with sliding effect (rotation)
getprop 属性
What is the meaning of load balancing
Eksctl deploying AWS eks
Go reflection - go language Bible learning notes
Fusobacterium -- symbiotic bacteria, opportunistic bacteria, oncobacterium
Record the blind injection script
Migrate from MySQL database to AWS dynamodb