使用C++标准类库的智能指针(源代码)
2009-06-29 10:57
441 查看
#include "stdafx.h" #include <iostream> #include <string> #include <cstring> #include <memory> using namespace std; class CMyStr { public: CMyStr(const char* pstr) { cout << "CMyStr::CMyStr()" << endl; m_pData = NULL; if (NULL == pstr) { cout << "param is null!"; return; } int nLen = strlen(pstr) + 1; m_pData = new char[nLen]; if (NULL == m_pData) { cout << "get memory failed..." << endl; return; } strncpy(m_pData, pstr, nLen); } ~CMyStr() { cout << "CMyStr::~CMyStr()" << endl; if (NULL != m_pData) { delete[] m_pData; m_pData = NULL; } } const char* c_str() { return m_pData; } private: char* m_pData; }; int main() { auto_ptr<CMyStr> pStr(new CMyStr("hello, andylin!")); cout << pStr->c_str() << endl; auto_ptr<string> pstr2(new string("I love my baby so much!")); cout << pstr2->c_str() << endl; return 0; }
相关文章推荐
- C++中智能指针的设计和使用
- C++几个技巧:智能指针在消息传递中的使用,元组,及lambda删除器
- C++中智能指针的设计和使用
- C++中智能指针的设计和使用
- C++中智能指针的设计和使用
- C++中智能指针的设计和使用
- C++中智能指针的设计和使用
- C++中智能指针的设计和使用
- c++ shared_ptr智能指针使用注意事项
- C++中智能指针的设计和使用 http://blog.csdn.net/hackbuteer1/article/details/7561235
- C++中智能指针的设计和使用【转】
- c++ shared_ptr智能指针使用注意事项
- 有效的使用和设计COM智能指针——条款20:安全的覆盖掉C++默默为我们编写的函数
- C++中智能指针的设计和使用
- C++中智能指针的设计和使用
- C++:指向数组的智能指针scoped_array组件的使用
- 实战c++中的智能指针unique_ptr系列-- 使用std::unique_ptr代替new operator(错误:‘unique_ptr’ is not a member of ‘std’)
- C++中智能指针的设计和使用
- C++ 使用智能指针
- [C++]智能指针的实现与使用