The same way std::string does it
class String
{
public:
String();
String(char *str);
~String();
String& operator= (char *str);
operator char* ();
const char* c_str() {return szStr;}
bool operator== (char *str);
private:
char *szStr;
};
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
1. By the way, you must define non-trivial copy constructor and assignment operator for this class String (or declare them as private to prevent inevitable program crash if default copy constructor and/or assignment will be called).
2. If you want String to std::string conversion, define it, what's a problem?
String::operator std::string() const
{
return szStr;
}
3. Avoid using of malloc/strdup/free stuff in C++ programs. Use new/delete operators only.
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348