Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confused about constant objects C++

Tags:

c++

visual-c++

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 ;)

like image 866
Sudantha Avatar asked Jan 22 '23 02:01

Sudantha


1 Answers

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.)

like image 78
bgporter Avatar answered Jan 30 '23 14:01

bgporter