浏览 3039 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (3) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2010-06-02
#include <stdlib.h> #include <string.h> class TSimpleString { public: static const size_t npos; TSimpleString() : m_pStorage(NULL) {} TSimpleString(const TSimpleString& s) { assign(s.m_pStorage); } TSimpleString(const char * s, size_t n = npos) { assign(s, n); } bool empty(void) { return (m_pStorage == NULL); } void clear(void); void assign(const char * s, size_t n = npos); const char * c_str(void) { return (m_pStorage == NULL) ? "" : m_pStorage; } private: char * m_pStorage; }; void TSimpleString::clear() { if( m_pStorage != NULL ) delete m_pStorage; } void TSimpleString::assign(const char * s, size_t n) { if( m_pStorage == s ) return; clear(); if( n == npos ) n = ( s == NULL ? 0 : strlen(s) ); if( n > 0 ) { m_pStorage = new char [n]; for( size_t i = 0; i < n; ++i ) m_pStorage[i] = s[i]; } } 提示: 1,有1处编译错误 2,大概有8处严重错误 3,有2处可改进的地方 注:可能有些错误我自己还没发现 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2010-06-19
12行 改为 : return (p_mStorage==NULL || strlen(p_mStorage)==0);
24行后面添加一句 : m_pStorage=NULL; 38行 改为 : m_pStorage = new char [n+1]; 39-40那块赋值语句可以改为 strncpy (m_pStorage,s,n); 40行 后面添加 : m_pStorage[i]='\0'; 其他的嘛,暂时还没看出来。。。。 |
|
返回顶楼 | |
发表时间:2010-06-19
PS:好像没有编译错误啊
|
|
返回顶楼 | |
发表时间:2010-06-29
电热熔突然通过人头
|
|
返回顶楼 | |