All books and internet pages I read say that C++ constructors do not have a return value and they just initialize an object:
#include <iostream>
class Number {
int m_val{};
public:
Number() = default;
Number(int val) : m_val(val) {}
int val() { return m_val; }
};
int main() {
Number n; // Initializing object with defualt constructor
std::cout << n.val() << '\n';
return 0;
}
But it turns out that also I can use constructors for assignment and for calling methods of object, like it returns the value of this object:
Number n = Number(10); // This works
std::cout << Number(29).val() << '\n'; // And this
In other similar stackoverflow questions like this and this people write that this semantics creates a value-initialized temporary object of type Number
, but it does not answer my question.
So does constructor return object, or maybe it is some c++ entity that i've never heard of?