I was going through the following question :
Temporary objects - when are they created, how do you recognise them in code?
In the code block of the question above , :which goes as follow :
//: C08:ConstReturnValues.cpp
// Constant return by value
// Result cannot be used as an lvalue
class X {
int i;
public:
X(int ii = 0);
void modify();
};
X::X(int ii) { i = ii; }
void X::modify() { i++; }
X f5() {
return X();
}
const X f6() {
return X();
}
void f7(X& x) { // Pass by non-const reference
x.modify();
}
int main() {
f5() = X(1); // OK -- non-const return value
f5().modify(); // OK
// Causes compile-time errors:
//! f7(f5());
//! f6() = X(1);
//! f6().modify();
//! f7(f6());
}
I could not identify what exactly is the following part in it :
X f5() {
return X();
}
const X f6() {
return X();
}
void f7(X& x) { // Pass by non-const reference
x.modify();
}
What is happening in the above part ?
I think the part :
X f5() {
return X();
}
declares and defines a function f5 which returns an object of class X . However I am not sure about the part
return X()
Is it declaring an object of type X using a constructor without any arguments ?
Is my thinking about the above two code snippets correct or these are something different and are known by some term or concept in C++ , if yes what concept to read into for understanding that ?