Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any difference between const Class& and Class const&?

Is there any difference between that expressions :

const Class&
Class const&

for example, when those are parameters of function ?

like image 204
KittyT2016 Avatar asked Feb 02 '16 12:02

KittyT2016


People also ask

Can a class be const?

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.

What does const mean in C++ classes?

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.

Why const correctness?

The benefit of const correctness is that it prevents you from inadvertently modifying something you didn't expect would be modified.

How do you make a class const in C++?

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.


1 Answers

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.

like image 127
Madhav Datt Avatar answered Oct 22 '22 23:10

Madhav Datt