006
01.03.2004, 11:55 Uhr
~(un)wissender
Gast
|
Die hatte ich bei mir noch rumfliegen, vielleicht hilft die. Ist nicht zwangsläufig korrekt, hatte die, glaube ich, nicht ausgiebig getestet, sonder nur zu Übungszwecken geschrieben. Wenn du beim operator>> was Tolleres willst (also nicht std::string), dann musst du, um die gleiche Funktionalität zu bekommen, etwas Betriebssystemabhängiges machen.
C++: |
#ifndef STRING_INC #define STRING_INC
#include <string> #include <cString>
class String { private: int capacity; int length; char *cString;
public: String(const String &other); String(const char *otherCString); String(int amount = 1); ~String(); inline bool operator==(const String &other) const; String& operator=(const String &other); inline friend String operator+(const String &first, const String &second); friend std::ostream &operator<<(std::ostream &out, const String &str); friend std::istream &operator>>(std::istream &in, String &str); String& operator+=(const String &other); inline const char& operator[](int index) const; inline char& operator[](int index); inline int getCapacity() const; inline int getLength() const; void reserve(int amount); void trim(); inline const char* c_str() const; };
#endif
String::String(const String &other) { int size = other.length; length = capacity = size; cString = new char[size]; strcpy(cString, other.cString); }
String::String(const char *otherCString) { int size = static_cast<int>(strlen(otherCString) + 1); cString = new char[size]; strcpy(cString, otherCString); length = capacity = size; }
String::String(int amount) { length = 1; capacity = amount; cString = new char[amount]; cString[0] = '\0'; }
String::~String() { delete [] cString; }
bool String::operator==(const String &other) const { return strcmp(cString, other.cString) == 0; }
String& String::operator=(const String &other) { if(this == &other) return *this;
if(capacity < other.length) { delete [] cString; cString = new char[other.length]; capacity = length = other.length; }
strcpy(cString, other.cString); length = other.length; return *this;
}
String operator+(const String &first, const String &second) { return String(first) += second; }
String& String::operator+=(const String &other) { int size = length + other.length - 1; if(capacity < size) { reserve(size); } length = size; strcat(cString, other.cString); return *this;
}
std::ostream& operator<<(std::ostream &out, const String &str) { return out << str.cString; }
std::istream& operator>>(std::istream &in, String &str) { std::string stream; getline(in, stream); str = stream.c_str(); return in; }
const char& String::operator[](int index) const { return cString[index]; }
char& String::operator[](int index) { return cString[index]; }
int String::getCapacity() const { return capacity; }
int String::getLength() const { return length; }
void String::reserve(int amount) { if(amount > capacity) { char *temp = new char[amount]; strcpy(temp, cString); delete [] cString; cString = temp; capacity = amount; } }
void String::trim() { if(capacity > length) { char *temp = new char[length]; strcpy(temp, cString); delete [] cString; cString = temp; capacity = length; } }
const char* String::c_str() const { return cString; }
int main() { using namespace std; String test = "Hallo"; String test1 = ", du!"; cout << "\n" << test + test1 << endl; return 0; }
|
|