`
wengshanjin
  • 浏览: 22669 次
  • 性别: Icon_minigender_1
  • 来自: 广州
最近访客 更多访客>>
社区版块
存档分类
最新评论

原创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); }

	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处可改进的地方

注:可能有些错误我自己还没发现
分享到:
评论
3 楼 wyong1021 2010-06-29  
电热熔突然通过人头
2 楼 xuqin1019 2010-06-19  
PS:好像没有编译错误啊
1 楼 xuqin1019 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';

其他的嘛,暂时还没看出来。。。。

相关推荐

Global site tag (gtag.js) - Google Analytics