各位帮我看看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();
}