设计实现 发表于 2018-03-23 | 阅读次数: 设计实现 设计并实现一个LRU Cache 设计并实现一个LRU Cache 请简述智能指针原理,并实现一个简单的智能指针 参考答案 C++ 引用计数技术及智能指针的简单实现 C++智能指针简单剖析 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849class SmartPointer;template<typename T>class Counter { friend class SmartPointer<T>; private: T* ptr; int cnt; public: Counter() { ptr = NULL; cnt = 0; } Counter(T* p) { ptr = p; cnt = 1; } ~Counter() { delete ptr; }};template <typename T>class SmartPointer { private: Counter* ptr_counter; public: SmartPointer(T* p) { ptr_counter = new Counter(p); } SmartPointer(const SmartPointer &sp) { ptr_counter = sp.ptr_counter; ++ptr_count->cnt; } SmartPointer& operator=(const SmartPointer &sp) { ++sp.ptr_counter->cnt; --ptr_counter->cnt; if (ptr_counter->cnt == 0) { delete ptr_counter; } ptr_counter = sp.ptr_counter; } ~SmartPointer() { --ptr_counter->cnt; if (ptr_counter->cnt == 0) { delete ptr_counter; } }}; 实现一个Memcpy函数 实现memcpy函数(代码较好,基本正确) 实现一个Memcpy函数(评论中有一个按照int 字节拷贝,效率提升,值得借鉴。)