Is there any difference between that expressions :
const Class&
Class const&
for example, when those are parameters of function ?
User-defined types, including classes, structs, and arrays, cannot be const . Use the readonly modifier to create a class, struct, or array that is initialized one time at run time (for example in a constructor) and thereafter cannot be changed.
The const member functions are the functions which are declared as constant in the program. The object called by these functions cannot be modified. It is recommended to use const keyword so that accidental changes to object are avoided.
The benefit of const correctness is that it prevents you from inadvertently modifying something you didn't expect would be modified.
A const object can be created by prefixing the const keyword to the object declaration. Any attempt to change the data member of const objects results in a compile-time error.
There is no difference between const Class&
and Class const&
; similarly here is no difference between const Class*
and Class const*
.
And so,
void f1 (const Class& c)
and
void f1 (Class const& c)
are interchangeable with no difference.
Both versions denote a reference to a constant Class instance and can be used interchangeably.
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