摘要: /* 优化string 复制的时候 仅复制引用,只有在修改内容时,才复制内容 即实现写时拷贝 */ class COWMyString { public: //默认参数 COWMyString(const char *str = "") :m_str(strcpy(new char[strlen(s 阅读全文
posted @ 2021-03-17 17:01 Truman001 阅读(195) 评论(0) 推荐(0) 编辑
摘要: class A { public: //静态函数,返回引用 static A &GetInstance() {//静态局部变量 static A s_instance; return s_instance; } private: //默认构造函数 A() = default; /* 拷贝构造函数 用 阅读全文
posted @ 2021-03-17 16:13 Truman001 阅读(57) 评论(0) 推荐(0) 编辑
摘要: /* 实现一个string满足基本用法 */ class MyString { public: //默认参数 MyString(const char *str=""):m_str(strcpy(new char[strlen(str)+1], str)) { } ~MyString(void) { 阅读全文
posted @ 2021-03-17 16:12 Truman001 阅读(93) 评论(0) 推荐(0) 编辑
摘要: /* 实现一个线程安全的队列 */ template <class T> class SafeQueue { public: SafeQueue(void):q(),m(),c() {} ~SafeQueue(void) {} // Add an element to the queue. void 阅读全文
posted @ 2021-03-17 16:06 Truman001 阅读(549) 评论(0) 推荐(0) 编辑
摘要: /* 智能指针的简单实现 */ template<typename T> class SamrtPtr { public: //构造函数 SamrtPtr(T* ptr = nullptr) :m_ptr(ptr) { if (m_ptr) { m_count = new size_t(1); } 阅读全文
posted @ 2021-03-17 16:04 Truman001 阅读(86) 评论(0) 推荐(0) 编辑