lightatdawn
#include <windows.h>//for BOOL #include <stdio.h> //for printf //------------------------------------------ // OVERLOADING OPERATORS: // Talk about customization! //------------------------------------------ /* I apologise for this example. I pulled it directly from an application that I'm writting and stripped it down some to make it less daunting and more readable. I havent added any coments beyond what already existed, as I've run out of time. Perhaps I'll get back to it in the future. For now I think the use of operator overloading is fairly apparent here. Have a look. I just overloaded some common operators to perform intuitive operations on a customized string storing class. Its useful for certain things; Mainly the fact that even a monkey couldnt screw it up. Notice that the overloaded operators ( =, +=, !=, ==, and () ) all act and look just like normal functions. Enjoy. */ /////////////////////////////////////////////////////////////////// // _SafeString /////////////////////////////////////////////////////////////////// class _SafeString { public: _SafeString() { String = NULL; }; ~_SafeString() { if (String != NULL) delete [] String; }; //assign value to string void operator= (char * ThisString) { AllocToSize(strlen(ThisString)); strcpy(String, ThisString); }; //append value to string void operator+= (char * ThisString) { if (String != NULL) { ExpandToSize(strlen(ThisString) + strlen(String)); strcat(String, ThisString); } else //idiot. Using Append before using New { AllocToSize(strlen(ThisString)); strcpy(String, ThisString); } }; //compare string BOOL operator== (char * ThisString) { if (strcmp(String, ThisString) == 0) return TRUE; return FALSE; }; //compare != string BOOL operator!= (char * ThisString) { if (strcmp(String, ThisString) == 0) return FALSE; return TRUE; }; //get the string length int len() { if (String != NULL) return strlen(String); return 0; }; //returns pointer to the string char * operator() () { if (String == NULL) return "MemError: String !Exist"; return String; }; private: void AllocToSize(unsigned short NewSize); void ExpandToSize(unsigned short NewSize); char * String; }; /////////////////////////////////////////////////////////////////// // _SafeString /////////////////////////////////////////////////////////////////// void _SafeString::AllocToSize(unsigned short NewSize) { //Remember the previous location in memory that String pointed to //so we can clean it up at the end char * pOldString; pOldString = String; //Point string to new memory of requested size String = new char[NewSize + 1]; //+1 to allow to NULL termination //delete the old memory //we dont copy any data because we're resetting the string if (pOldString != NULL) delete [] pOldString; } //----------------------------------------------------------------------- void _SafeString::ExpandToSize(unsigned short NewSize) { //Get the length of the previous string (if it exists) unsigned short OldSize = len() + 1; //length of current string //Remember the previous location in memory that String pointed to //so we can clean it up at the end char * pOldString; pOldString = String; //Point string to new memory of requested size String = new char[NewSize + 1]; //+1 to allow to NULL termination //copy over the old data to the new location (if it exists) if (pOldString != NULL) { memcpy (String, pOldString, OldSize); //chars size is 1 so we dont need to multiple by sizeof(char) //delete the old memory delete [] pOldString; } } //----------------------------------------------------------------------- //------------------------------------------ // MAIN //------------------------------------------ int main(void) { _SafeString Text; Text = "Hello"; Text += " World!"; if (Text == "Hello World!") printf("%s\n", Text() ); else printf("Something failed!\n"); return 0; }