While trying to remove warnings from c++ project i failed to understand why first function returning int gives warning "warning: type qualifiers ignored on function return type" but second function returning std::string doesn't give this warning ?
//first function
const int getX()
{
int x =9;
return x;
}
//second function
const std::string getTemp()
{
std::string test = "Test..";
return test;
}
const
on a primitive type has no effect for a return value, because you can't modify an rvalue of a primitive type.
getX() = 5; // error, even if getX() returns a non-const int.
For a class type however, const can make a difference:
std::string s = getTemp().append("extra");
// error if getTemp() returns a `const std::string`, but valid
// if getTemp() returns a non-const `std::string`.
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