diff options
Diffstat (limited to 'string.h')
-rw-r--r-- | string.h | 27 |
1 files changed, 19 insertions, 8 deletions
@@ -17,11 +17,14 @@ class MyString char *str_; int length_; MyString(int); + static size_t count; public: MyString(); - MyString(char); + explicit MyString(char); + /* allow implicit conversions, it is useful */ MyString(const char *); + /* copy constructor */ MyString(const MyString &); MyString& operator=(const MyString &); ~MyString(); @@ -33,29 +36,33 @@ class MyString MyString& concat(const MyString &); MyString& concat(const char *); void printStr(void) const; + static size_t getCount(void); bool operator!(void) const; char& operator[](int); char operator[](int) const; operator char*(); /* outward class conversion into char * */ + operator const char*() const; }; bool operator==(const MyString&, const MyString &); +bool operator!=(const MyString&, const MyString &); std::ostream& operator<<(std::ostream&, const MyString &); -inline MyString::MyString() +/* it is always good idea to have default ctor to avoid manual init later + * for data types that include this class type + */ +inline MyString::MyString() : str_(new char[1]), length_(0) { - length_ = 0; - str_ = new char[1]; str_[0] = '\0'; + this->count++; } -inline MyString::MyString(char ch) +inline MyString::MyString(char ch) : str_(new char[2]), length_(1) { - length_ = 1; - str_ = new char[2]; str_[0] = ch; str_[1] = '\0'; + this->count++; } inline int MyString::length() const @@ -68,5 +75,9 @@ inline void MyString::printStr(void) const std::cout << "length: " << length_ << ", value: \"" << str_ << "\""; } -#endif +inline size_t MyString::getCount(void) +{ + return count; +} +#endif |