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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With