ShenDaowu ShenDaowu - 1 year ago 94
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;

return anything? If it does, that's a value or a reference?

Answer Source

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"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download