Return value for a << operator function of a custom string class in C++
I am trying to create my own std::string wrapper to extend its functionality. But I got a problem when declaring the << operator. Here's my code so far:
my custom string class:
class MyCustomString : private std::string { public: std::string data; MyCustomString() { data.assign(""); } MyCustomString(char *value) { data.assign(value); } void Assign(char *value) { data.assign(value); } // ...other useful functions std::string & operator << (const MyCustomString &src) { return this->data; } };
the main program:
int main() { MyCustomString mystring("Hello"); std::cout << mystring; // error C2243: 'type cast' : conversion from 'MyCustomString *' to 'const std::basic_string<_Elem,_Traits,_Ax> &' exists, but is inaccessible return 0; }
I wanted cout to treat the class as a std::string, so that I won't need to do something like:
std::cout << mystring.data;
Any kind of help would be appreciated!
Thanks.
Just fyi: my IDE is Microsoft Visual C++ 2008 Express Edition.
Firstly, you seem to have an issue with the definition of MyCustomString. It inherits privately from std::string
as well as containing an instance of std::string
itself. I'd remove one or the other.
Assuming you are implementing a new string class and you want to be able to output it using std::cout
, you'll need a cast operator to return the string data which std::cout
expects:
operator const char *()
{
return this->data.c_str();
}
If you look at how all stream operators are declared they are of the form:
ostream& operator<<(ostream& out, const someType& val );
Essentially you want your overloaded function to actually do the output operation and then return the new updated stream operator. What I would suggest doing is the following, note that this is a global function, not a member of your class:
ostream& operator<< (ostream& out, const MyCustomString& str )
{
return out << str.data;
}
Note that if your 'data' object was private, which basic OOP says it probably should, you can declare the above operator internally as a 'friend' function. This will allow it to access the private data variable.
你需要一个独立的功能(你的班级的朋友,如果你的data
私人的,你可能应该这么做!)
inline std::ostream & operator<<(std::ostream &o, const MyCustomString&& d)
{
return o << d.data;
}
链接地址: http://www.djcxy.com/p/67324.html