summaryrefslogtreecommitdiffstats
path: root/string.h
diff options
context:
space:
mode:
authorKamil Kaminski <kylek389@gmail.com>2014-02-28 03:55:14 -0600
committerKamil Kaminski <kylek389@gmail.com>2014-02-28 03:58:28 -0600
commitdd0127d725d2aba3c81e502d4ae03a14e5f2eef9 (patch)
tree0579d7941c1cd0c2f16300cc95dbeef46dd03d97 /string.h
parent401ed2b66c9e80eecaf154f3a2ef932f919ae456 (diff)
downloadcstring++-dd0127d725d2aba3c81e502d4ae03a14e5f2eef9.tar.gz
cstring++-dd0127d725d2aba3c81e502d4ae03a14e5f2eef9.tar.bz2
cstring++-dd0127d725d2aba3c81e502d4ae03a14e5f2eef9.zip
clarify obj creation, declare static field for....HEADmaster
counting, introduce outward conversion to 'const char *' without a copy, implement != operator
Diffstat (limited to 'string.h')
-rw-r--r--string.h27
1 files changed, 19 insertions, 8 deletions
diff --git a/string.h b/string.h
index dbc8e88..2e86086 100644
--- a/string.h
+++ b/string.h
@@ -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