I always believed that temporary objects in C++ are automatically considered as const by the compiler. But recently I experienced that the following example of code:
function_returning_object().some_non_const_method();
is valid for C++ compiler. And it makes me wonder - are temporary objects in C++ const indeed? If yes, then why the code above is considered correct by the compiler?
No, they're not. Not unless you declare the return type as const.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With