当前位置:网站首页>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's the difference between error and exception
- Teach you how to build the ruoyi system by Tencent cloud
- 解决ValueError: Argument must be a dense tensor: 0 - got shape [198602], but wanted [198602, 16].
- selenium模式下切换窗口,抓取数据的实现
- Alibaba tip: it is better to create threads manually
- Small volume Schottky diode compatible with nsr20f30nxt5g
- test
- A lifetime of needs, team collaboration can play this way on cloud nailing applet
- leetcode008--实现strStr()函数
- L2-011 玩转二叉树(建树+BFS)
猜你喜欢

补充番外14:cmake实践项目笔记(未完待续4/22)

test

Unity rawimage background seamlessly connected mobile

那些年我面试过的Android开发岗总结(附面试题+答案解析)

AWS eks add cluster user or Iam role

Ali's ten-year technical experts jointly created the "latest" jetpack compose project combat drill (with demo)

Windows remote connection to redis

Supplément: annotation

/etc/bash_ completion. D directory function (the user logs in and executes the script under the directory immediately)

How to regulate intestinal flora? Introduction to common natural substances, probiotics and prebiotics
随机推荐
The programmer starts the required application with one click of window bat
Create VPC in AWS console (no plate)
win10, mysql-8.0.26-winx64. Zip installation
IEEE Transactions on systems, man, and Cybernetics: Notes for systems (TSMC)
thymeleaf th:value 为null时报错问题
[paper reading] [3D object detection] voxel transformer for 3D object detection
PIP3 installation requests Library - the most complete pit sorting
getprop 属性
Huawei machine test -- high precision integer addition
Recommended scheme for national production of electronic components for wireless charging
The perfect combination of collaborative process and multi process
A lifetime of needs, team collaboration can play this way on cloud nailing applet
What is a blocking queue? What is the implementation principle of blocking queue? How to use blocking queue to implement producer consumer model?
简单的拖拽物体到物品栏
IEEE Transactions on industrial information (TII)
SQL statement for adding columns in MySQL table
Leetcode009 -- search the target value in the array with binary search
補:注解(Annotation)
No such file or directory problem while executing shell
leetcode006--查找字符串数组中的最长公共前缀