ShenDaowu ShenDaowu - 3 months ago 24
C++ Question

Does string("abc") return anything?

like this:

#include <iostream>
using namespace std;
int main()
{
if ( string( "abc" ).size() == 3 )
cout << "equ." << endl;
return 0;
}


Does
string("abc")
return anything? If it does, that's a value or a reference?

Answer

Class constructors do not have specified return type, and do not have return statement. But they do have implicit return type - same as class in which they are declared and constructor "returns" just constructed object.

T() calls default constructor and "returns" anonymous prvalue of type T (which binds to T&&). Its lifetime is lifetime of expression in which it is used. It is modifiable.

Use example:

int()                                           // 0
(string()+=string("123")+="abc").substr(1,3)    // "23a"