Explain this C ++ statement definition

I have the following statement defined in a C ++ class called StringProxy:

operator std::string&()
{
    return m_string;
}

a) What is it and how does it work? I understand the idea of ​​operator overloading, but they usually look like X operator+(double i).

b) Given an instance StringProxy, how can I use this statement to get m_string?

+3
source share
2 answers

This is a conversion method. To get m_string, just use the explicit expression: (std::string)stringProxyto perform the conversion. Depending on the context (for example, if you assign a string), you can do without translation.

+3
source

. T() . std::string, .

+2

Source: https://habr.com/ru/post/1745210/


All Articles