各位帮我看看inline String String::operator + (const String &rhs) const的实现有问题吗?

arefe 2004-09-27 04:17:42
各位帮我看看inline String String::operator + (const String &rhs) const的实现有问题吗?
先谢谢了!
第一次在c/c++发帖子,来者有分:)

#include <iostream>
#include <cstring>
#include <cassert>
#include <iomanip>

using namespace std;

class String;
istream& operator >> (istream&, String&);
ostream& operator << (ostream&, const String&);

class String {
public:
// a set of overloaded constructors
// support auto intilize function
// String str1; // String()
// String str2("literal"); // String(const char*)
// String str3(str2); // String(const String&)
String();
String(const char*);
String(const String&);

// deconstructor
~String();

// a set of overloaded assignment operator
// str1 = str2
// str3 = "a string literal"
String& operator = (const String&);
String& operator = (const char*);

//LX3.29
inline String operator + (const String &rhs) const;

// a set of overloaded equal sign operator
// str1 == str2
// str3 == "a string literal"
bool operator == (const String&);
bool operator == (const char*);

// overloaded index operator
// str1[0] = str2[0]
char& operator [] (int);

// member access methods
int size() {return _size;}
char* c_str() { return _string;}

private:
int _size;
char *_string;
};

inline bool String::operator == (const String &rhs) {
if (_size != rhs._size )
return false;
return strcmp(_string, rhs._string ) ? false : true;
}

inline bool String::operator == (const char *s) {
return strcmp(_string, s ) ? false : true;
}

// default constructor
inline String::String() {
_size = 0;
_string = 0;
}

inline String::String(const char *str) {
if (str == 0) {
_size = 0;
_string = 0;
} else {
_size = strlen(str);
_string = new char[_size + 1];
strcpy(_string, str);
}
}

// copy constructor
inline String::String(const String &rhs) {
_size = rhs._size ;
if (rhs._string == 0) {
_string = 0;
} else {
_string = new char[_size + 1];
strcpy(_string, rhs._string);
}
}

//LX3.29
///////////////////////////
inline String String::operator + (const String &rhs) const
{
int len = _size + rhs._size;
char *new_str = new char[len + 1];
memset(new_str, NULL, len + 1);
strcat(new_str, _string);
strcat(new_str, rhs._string);
String ret_str (new_str);
delete[] new_str;
return ret_str;
}
/////////////////////////////

// destructor
inline String::~String () {delete [] _string;}

inline String& String::operator = (const char *s) {
delete [] _string;
if ( !s ) {
_size = 0;
_string = 0;
} else {
_size = strlen( s );
_string = new char[ _size + 1];
strcpy(_string, s);
}
return *this;
}

inline String& String::operator = (const String &rhs) {
if ( this != &rhs) {
delete [] _string;
_size = rhs._size;

if ( !rhs._string ) {
_string = 0;
} else {
_string = new char[_size + 1];
strcpy(_string, rhs._string);
}
}
return *this;
}

inline char& String::operator [] (int elem) {
assert(elem >= 0 && elem < _size);
return _string[elem];
}

inline istream& operator >> (istream &io, String &s) {
// max input 4095 chars
const int limit_string_size = 4096;
char inBuf[limit_string_size];

//
io >> setw(limit_string_size) >> inBuf;
s = inBuf; // String::operator=(const char *);
return io;
}

// why cannot use const??
//inline ostream& operator << (ostream & os, const String &s) {
inline ostream& operator << (ostream & os, String &s) {
return os << s.c_str();
}
...全文
262 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
hcj2002 2004-09-29
  • 打赏
  • 举报
回复
vc6 好像只有完全安装才有相关的源文件
:(
arefe 2004-09-29
  • 打赏
  • 举报
回复
我用的是vc6,c++的标准库源代码在哪里呢?

include目录下,只是头文件啊?!具体的实现文件呢?
daylove 2004-09-29
  • 打赏
  • 举报
回复
学习,

回复人: freefalcon(心宇—小小菜鸟想高飞) ( ) 信誉:100

======================================
就是厉害
柯本 2004-09-29
  • 打赏
  • 举报
回复
VC6好像没有stl源码,以下是STL的下载
http://www.stlport.org/download.html
http://www.sgi.com/tech/stl/download.html
haozaizi 2004-09-29
  • 打赏
  • 举报
回复
return strcmp(_string, rhs._string ) ? false : true;
是有问题,rhs._strin,好象是私有变量,你用友元就没有问题了
haozaizi 2004-09-29
  • 打赏
  • 举报
回复
在include 目录下
arefe 2004-09-29
  • 打赏
  • 举报
回复
我用的是vc6,c++的标准库源代码在哪里呢?
逸学堂 2004-09-29
  • 打赏
  • 举报
回复
改说的都说垃。学习
freefalcon 2004-09-29
  • 打赏
  • 举报
回复
STL差不多都是以模板方式提供,头文件有基本的实现
arefe 2004-09-29
  • 打赏
  • 举报
回复
好了,结贴了,谢谢各位了。
pacman2000 2004-09-28
  • 打赏
  • 举报
回复
一般都是写成友元函数的。
freefalcon 2004-09-28
  • 打赏
  • 举报
回复
编译器的安装目录下都有源文件

STL一般没有提供成员函数方式的operator+,而是提供友元,这有一个好处
因为如果是成员函数方式,你的左操作数必须是String类型,
而不支持
String str1("abc"), str2; str2 = "123" + str1;
这样的形式
用友元则可以,编译器会自动用"123"构造出一个临时对象

当然楼主的实现也没什么问题,不过这两句可以写成一句
memset(new_str, NULL, len + 1);
strcat(new_str, _string);
--〉
strcpy(new_str, _string);
柯本 2004-09-28
  • 打赏
  • 举报
回复
1.网上搜一下,很多的
2.如果你安装了BCB.在它根的\Source\Rtl\Source\stl中有stl的全部源码
arefe 2004-09-28
  • 打赏
  • 举报
回复
给点意见啊?谁告诉我怎么得到C++标准库的源代码?

64,660

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧