const before parameter vs const after function name c++
What is the difference betweeen something like this
friend Circle copy(const Circle &);
and something like this
friend Circle copy(Circle&) const;
I know const after the function is used to tell the compiler that this function won't attempt to change the object it is called on, what about the other one?
Solution 1:
The first form means that the (state of the) Circle
object bound to the reference which is the parameter of the copy()
function will not be altered by copy()
through that reference. The reference is a reference to const
, so it won't be possible to invoke member functions of Circle
through that reference which are not themselves qualified as const
.
The second form, on the other hand, is illegal: only member functions can be const
-qualified (while what you are declaring there is a global, friend
function).
When const
qualifies a member function, the qualification refers to the implicit this
argument. In other words, that function will not be allowed to alter the state of the object it is invoked on (the object pointed to by the implicit this
pointer) - with the exception of mutable
objects, but that's another story.
To say it with code:
struct X
{
void foo() const // <== The implicit "this" pointer is const-qualified!
{
_x = 42; // ERROR! The "this" pointer is implicitly const
_y = 42; // OK (_y is mutable)
}
void bar(X& obj) const // <== The implicit "this" pointer is const-qualified!
{
obj._x = 42; // OK! obj is a reference to non-const
_x = 42; // ERROR! The "this" pointer is implicitly const
}
void bar(X const& obj) // <== The implicit "this" pointer is NOT const-qualified!
{
obj._x = 42; // ERROR! obj is a reference to const
obj._y = 42; // OK! obj is a reference to const, but _y is mutable
_x = 42; // OK! The "this" pointer is implicitly non-const
}
int _x;
mutable int _y;
};
Solution 2:
C++ class methods have an implicit this
parameter which comes before all the explicit ones. So a function declared within a class like this:
class C {
void f(int x);
You can imagine really looks like this:
void f(C* this, int x);
Now, if you declare it this way:
void f(int x) const;
It's as if you wrote this:
void f(const C* this, int x);
That is, the trailing const
makes the this
parameter const, meaning that you can invoke the method on const objects of the class type, and that the method cannot modify the object on which it was invoked (at least, not via the normal channels).
Solution 3:
LET'S CLEAR ALL CONFUSION RELATED TO const
const
came from constant mean something is not changeable but readable.
-
if we qualify our variable with
const
keyword ,we can't change it later.
e.g.const
int var =25;
const variable must be initialized when it's declared.var =50; // gives error
-
if we qualify our pointer variable with
const
after*
then we can't change pointer itself but content of pointer is changeable.
e.g.int *
const
ptr = new int;
ptr = new int; //gives error
// but*ptr=5445; //allowed
-
if we qualify our pointer variable with
const
before*
then we can change pointer itself but content of pointer is not changeable.
e.g.int
const
* ptr = new int(85);
//or
const
int * ptr = new int(85);
ptr = new int; // allowed
// but*ptr=5445; // gives error
-
pointer and content both constant
e.g.int
const
*
const
ptr = new int(85);
//or
const
int *
const
ptr = new int(85);
ptr = new int; // not allowed
*ptr=5445; // not allowed
-
Circle copy(const Circle &);
here const Circle means value of Circle is only readable ,if we try to change value of Circle inside function then it gives error. -
friend Circle copy(Circle&) const;
This type of function is not for non member variable .it is used for class or structure. Here whole function is qualified with const keyword means we can't change object member variable . e.g
class A{ public :
int var;
void fun1()
{ var = 50; // allowed
}
void fun2()const
{ var=50; //not allowed
}
};
Solution 4:
Circle copy(Circle&) const;
makes the function const
itself. This can only be used for member functions of a class/struct.
Making a member function const
means that
- it cannot call any non-const member functions
- it cannot change any member variables.
- it can be called by a
const
object(const
objects can only callconst
functions). Non-const objects can also call aconst
function. - It must be member function of the class 'Circle'.
Now consider the next one:
Circle copy(const Circle &);
while this one means that the parameter passed cannot be changed within the function. It may or may not be a member function of the class.
NOTE: It is possible to overload a function in such a way to have a const
and non-const version of the same function.