wengshanjin 发表于 2013-2-4 22:09:37

原创C++笔试题,查找一个字符串类不正确的地方

#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); }boolempty(void) { return (m_pStorage == NULL); }voidclear(void);voidassign(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 ;for( size_t i = 0; i < n; ++i )m_pStorage = s;}}

提示:
1,有1处编译错误
2,大概有8处严重错误
3,有2处可改进的地方

注:可能有些错误我自己还没发现
页: [1]
查看完整版本: 原创C++笔试题,查找一个字符串类不正确的地方