#include #include #include #include #include #include #include #include class cString { private: char *s; public: cString(const cString &String); cString(const char *S = NULL, bool TakePointer = false); virtual ~cString(); operator const char * () const { return s; } // for use in (const char *) context const char * operator*() const { return s; } // for use in (const void *) context (printf() etc.) cString &operator=(const cString &String); }; // --- cString --------------------------------------------------------------- cString::cString(const cString &String) { s = String.s ? strdup(String.s) : NULL; } cString::cString(const char *S, bool TakePointer) { s = TakePointer ? (char *)S : S ? strdup(S) : NULL; } cString::~cString() { free(s); } cString &cString::operator=(const cString &String) { printf("%s called\n", __FUNCTION__); if(s!=String.s) { free(s); } s = String.s ? strdup(String.s) : NULL; return *this; } int main(int argc, char *argv[]) { cString hw("Hello World"); hw = hw; printf("%s\n", *hw); return 0; }