Reputation: 24450
I have the following operator defined in a C++ class called StringProxy
:
operator std::string&()
{
return m_string;
}
a) What is this and how does this work? I understand the idea of operator overloading, but they normally look like X operator+(double i)
.
b) Given an instance of StringProxy
, how can I use this operator to get the m_string
?
Upvotes: 3
Views: 532
Reputation: 146930
It's a cast operator. They take the form of operator T() and enable casting between custom types. You can get the std::string out by simply assigning it to a regular string or reference.
Upvotes: 2
Reputation: 43380
This is a conversion method. To get the m_string, simply use an explicit cast: (std::string)stringProxy
to perform the conversion. Depending on context (e.g. if you're assigning to a string), you may be able to do without the cast.
Upvotes: 3