I'm confused about constant objects in C++
When we have passed a constant object/ reference of a const object is that mean that we cannot edit the attribute values of that object ?
or if its not whats the mean or constant
in that is it 'refer' object' or 'attributes'
also when we return a constant object
declaring the function like
return_type function_name(parameters) const
{
}
the const
keyword is at end of the function is it syntax ? and why if we return a const
object shouldn't it be like follows
const return_type function_name(parameters)
{
}
Sorry if its a noob question ;)
This syntax:
return_type function_name(parameters) const
{
}
Indicates that function_name()
may be invoked for an instance of the class that is const. It doesn't have any effect on the const-ness of the return value.
const return_type function_name(parameters)
{
}
...indicates that the value being returned from function_name()
is const (and says nothing about the const-ness of the object having its member function called.)
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