Is there a difference between using const:
Cannot change the datatype but can change the value of a or b
int add(const int a, const int b);
Can change the datatype but cannot change the value of a or b
int add(int const a, int const b);
Cannot change the datatype and cannot change the value of a or b
int add(const int const a, const int const b);
Many thanks for any suggestions
I don’t know how one is supposed to changed the datatype of a variable in C++ …
‘const’ is a promise you make to the compiler about not modifying a value. It complains when you don’t (probably uncovering z bug in the process). It also helps it to do various optimizations.
Here are some const examples and what they mean :
f cannot change the value of ‘a’.
the same but written in a weird way
means nothing, gcc tells me ‘duplicate const’
f cannot change the value pointed to by pa
f cannot change the value of the pointer
f cannot change the value of the pointer nor the value pointed to
The member function f cannot modify its object
Hope it makes things clearer ..