difference between these functions and const correctness

I know that a const variable cant be changed.
I also understand that a const reference as a function argument cant be changed.

what im confused with is functions that return const. like what is the difference between

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// whats the difference between this
const int getresult() { return 13;}

// this way must be in a class for some reason
class test()
{

int getresult() const 
{
 return 13;
}
};

// and this

class test2()
{
const int getresult() const
{
return 13;
}
};


basically im pretty clear when const is used as a function argument but confused when either its at the start of a function or the end of a function and why does it have to be in a class to be at the end of a function.

In the case of these return values, there is no difference since you are returning an int by value. A const int can be assigned to anything, including a non-const int.

The const after the formal parameter list indicates it will not modify the calling object, and thus is suitable to be called from a const object.
Topic archived. No new replies allowed.