I'm re-kindling some of my C++ theory and was working through a C++ SringBuilder class I found on the net.
I understand it all except the 2 operator methods. (See listing below).
Can anyone explain what these operator methods are for and how and why you would impliment them.

class StringBuilder
{
private: std::basic_string<TCHAR> _fmtedString;


public: explicit StringBuilder()
        {}

public: StringBuilder(const TCHAR* pszFormat, ...)
        {
           TCHAR buf[1024];

           va_list marker;
           va_start(marker, pszFormat);

#ifdef UNICODE
           vswprintf_s(buf, pszFormat, marker);
#else
           vsprintf_s(buf, pszFormat, marker);
#endif

           _fmtedString = Convert::ToString(buf);
        }

public: operator std::basic_string<TCHAR>() const
        {
           return _fmtedString;
        }

public: operator const TCHAR*() const
        {
           return _fmtedString.c_str();
        }        


public: const TCHAR* ToString() const
        {
           return _fmtedString.c_str();
        }

public: void Append(const std::basic_string<TCHAR>& charStr)
        {
           _fmtedString.append(charStr);
        }


public: void AppendLine(const std::basic_string<TCHAR>& charStr = _TXT(""))
        {  
           _fmtedString.append(charStr);
           _fmtedString.append(NEW_LINE);
        }
};

Thanks in advance ... Milton

public: operator std::basic_string<TCHAR>() const
        {
           return _fmtedString;
        }

This means that the class can be interpreted as a basic_string when used.

public: operator const TCHAR*() const
        {
           return _fmtedString.c_str();
        }        

In this case it will be interpreted as a TCHAR* pointer

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.