当前位置:网站首页>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
边栏推荐
- Unity3d practical skills - theoretical knowledge base (I)
- Chapter 4 - understanding standard equipment documents, filters and pipelines
- Phishing for NFT
- FAQ of foreign lead and alliance Manager
- IDE idea automatic compilation and configuration of on update action and on frame deactivation
- win10, mysql-8.0.26-winx64.zip 安装
- Druid -- JDBC tool class case
- Simply drag objects to the item bar
- Installation and use of Apache bench (AB pressure test tool)
- Unity rawimage background seamlessly connected mobile
猜你喜欢
Record the ThreadPoolExecutor main thread waiting for sub threads
win10, mysql-8.0.26-winx64.zip 安装
Simply drag objects to the item bar
Installation and use of Apache bench (AB pressure test tool)
Programmers complain: I really can't live with a salary of 12000. Netizen: how can I say 3000
Luogu p1858 [multi person knapsack] (knapsack seeking the top k optimal solution)
Go reflection rule
Recommended scheme for national production of electronic components of wireless keyboard
补充番外14:cmake实践项目笔记(未完待续4/22)
Spark small case - RDD, spark SQL
随机推荐
How to regulate intestinal flora? Introduction to common natural substances, probiotics and prebiotics
Recursive call -- Enumeration of permutations
What's the difference between error and exception
[paper reading] [3D object detection] voxel transformer for 3D object detection
AWS eks add cluster user or Iam role
Coinbase: basic knowledge, facts and statistics about cross chain bridge
The unity camera rotates with the mouse
229. Find mode II
2020 is coming to an end, special and unforgettable.
简单的拖拽物体到物品栏
Leetcode - > 1 sum of two numbers
Basic operation of sequence table
拼了!两所A级大学,六所B级大学,纷纷撤销软件工程硕士点!
IDE idea automatic compilation and configuration of on update action and on frame deactivation
Flink's important basics
Code007 -- determine whether the string in parentheses matches
Installation and use of Apache bench (AB pressure test tool)
QML进阶(四)-绘制自定义控件
Apache Bench(ab 压力测试工具)的安装与使用
Use recyclerview to realize left-right side-by-side classification selection