模板学习实践二 pointer
c++ template学习记录
使用模板将实际类型的指针进行封装
当变量退出作用域 自动delete
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | // 1111.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" template <typename T> class Holder { private : T* ptr; // refers to the object it holds (if any) public : // default constructor: let the holder refer to nothing Holder() : ptr(0) { } // constructor for a pointer: let the holder refer to where the pointer refers explicit Holder(T* p) : ptr(p) { } // destructor: releases the object to which it refers (if any) ~Holder() { delete ptr; } // assignment of new pointer Holder<T>& operator = (T* p) { delete ptr; ptr = p; return * this ; } // pointer operators T& operator * () const { return *ptr; } T* operator -> () const { return ptr; } // get referenced object (if any) T* get () const { return ptr; } // release ownership of referenced object void release() { ptr = 0; } // exchange ownership with other holder void exchange_with(Holder<T>& h) { std::swap(ptr, h.ptr); } // exchange ownership with other pointer void exchange_with(T*& p) { std::swap(ptr, p); } private : // no copying and copy assignment allowed Holder(Holder<T> const &); Holder<T>& operator = (Holder<T> const &); }; class Something { public : void perform() const { } }; void do_two_things() { Holder<Something> first( new Something); first->perform(); Holder<Something> second( new Something); second->perform(); } int main() { do_two_things(); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 | // 1111111.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <stddef.h> #include <iostream> #include <vector> using namespace std; size_t* alloc_counter() { return :: new size_t; } void dealloc_counter(size_t* ptr) { ::delete ptr; } class SimpleReferenceCount { private : size_t* counter; // the allocated counter public : SimpleReferenceCount() { counter = NULL; } // default copy constructor and copy-assignment operator // are fine in that they just copy the shared counter public : // allocate the counter and initialize its value to one: template<typename T> void init(T*) { counter = alloc_counter(); *counter = 1; } // dispose of the counter: template<typename T> void dispose(T*) { dealloc_counter(counter); } // increment by one: template<typename T> void increment(T*) { ++*counter; } // decrement by one: template<typename T> void decrement(T*) { --*counter; } // test for zero: template<typename T> bool is_zero(T*) { return *counter == 0; } }; class StandardArrayPolicy { public : template<typename T> void dispose(T* array) { delete[] array; } }; class StandardObjectPolicy { public : template<typename T> void dispose(T* object ) { delete object ; } }; template<typename T, typename CounterPolicy = SimpleReferenceCount, typename ObjectPolicy = StandardObjectPolicy> class CountingPtr : private CounterPolicy, private ObjectPolicy { private : // shortcuts: typedef CounterPolicy CP; typedef ObjectPolicy OP; T* object_pointed_to; // the object referred to (or NULL if none) public : // default constructor (no explicit initialization): CountingPtr() { this ->object_pointed_to = NULL; } // a converting constructor (from a built-in pointer): explicit CountingPtr(T* p) { this ->init(p); // init with ordinary pointer } // copy constructor: CountingPtr(CountingPtr<T, CP, OP> const & cp) : CP((CP const &)cp), // copy policies OP((OP const &)cp) { this ->attach(cp); // copy pointer and increment counter } // destructor: ~CountingPtr() { this ->detach(); // decrement counter // (and dispose counter if last owner) } // assignment of a built-in pointer CountingPtr<T, CP, OP>& operator = (T* p) { // no counting pointer should point to *p yet: assert(p != this ->object_pointed_to); this ->detach(); // decrement counter // (and dispose counter if last owner) this ->init(p); // init with ordinary pointer return * this ; } // copy assignment (beware of self-assignment): CountingPtr<T, CP, OP>& operator = (CountingPtr<T, CP, OP> const & cp) { if ( this ->object_pointed_to != cp.object_pointed_to) { this ->detach(); // decrement counter // (and dispose counter if last owner) CP:: operator =((CP const &)cp); // assign policies OP:: operator =((OP const &)cp); this ->attach(cp); // copy pointer and increment counter } return * this ; } // the operators that make this a smart pointer: T* operator -> () const { return this ->object_pointed_to; } T& operator * () const { return * this ->object_pointed_to; } // additional interfaces will be added later //... private : // helpers: // - init with ordinary pointer (if any) void init(T* p) { if (p != NULL) { CounterPolicy::init(p); } this ->object_pointed_to = p; } // - copy pointer and increment counter (if any) void attach(CountingPtr<T, CP, OP> const & cp) { this ->object_pointed_to = cp.object_pointed_to; if (cp.object_pointed_to != NULL) { CounterPolicy::increment(cp.object_pointed_to); } } // - decrement counter (and dispose counter if last owner) void detach() { if ( this ->object_pointed_to != NULL) { CounterPolicy::decrement( this ->object_pointed_to); if (CounterPolicy::is_zero( this ->object_pointed_to)) { // dispose counter, if necessary: CounterPolicy::dispose( this ->object_pointed_to); // use object policy to dispose the object pointed to: ObjectPolicy::dispose( this ->object_pointed_to); } } } }; void test1() { std::cout << "\ntest1():\n" ; CountingPtr< int > p0; { CountingPtr< int > p1( new int (42)); std::cout << "*p1: " << *p1 << std::endl; *p1 = 17; std::cout << "*p1: " << *p1 << std::endl; CountingPtr< int > p2 = p1; std::cout << "*p2: " << *p2 << std::endl; *p1 = 33; std::cout << "*p2: " << *p2 << std::endl; p0 = p2; std::cout << "*p0: " << *p0 << std::endl; ++*p0; ++*p1; ++*p2; std::cout << "*p0: " << *p0 << std::endl; std::cout << "*p1: " << *p1 << std::endl; std::cout << "*p2: " << *p2 << std::endl; } std::cout << "after block: *p0: " << *p0 << std::endl; } void test2() { std::cout << "\ntest2():\n" ; { CountingPtr< int > p0( new int (42)); CountingPtr< int > p2 = p0; } CountingPtr< int > p1( new int (42)); std::cout << "qqq" << std::endl; std::vector<CountingPtr< int > > coll; std::cout << "qqq" << std::endl; coll.push_back(p1); std::cout << "qqq" << std::endl; coll.push_back(p1); std::cout << "qqq" << std::endl; std::cout << "qqq" << std::endl; ++*p1; ++*coll[0]; std::cout << *coll[1] << std::endl; } int main() { test1(); test2(); } |
作 者: itdef
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力


【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话