const before parameter vs const after function name c++
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.
Solution 5
One refers to the parameter the other to the function.
Circle copy(const Circle &);
This means that the parameter passed in cannot be changed within the function
Circle copy(Circle&) const;
The const
qualified function is used for member functions and means you cannot change the data members of the object itself. The example you posted was nonsensical.
Read right-to-left
If we rewrite the first function as Circle copy(Circle const&);
, which means the same thing, it becomes clear that reading right to left becomes useful. copy
is a function that takes a const
reference to a Circle
object and returns a Circle
object by reference.
jazzybazz
Updated on July 21, 2020Comments
-
jazzybazz almost 4 years
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?
-
mah about 11 yearsPerfectly correct however it doesn't answer the question, which isn't actually referring to a class method but rather a friend function.
-
John Zwinck about 11 yearsYeah, I chose to ignore the
friend
part because I think it's actually irrelevant to the OP's real question (or what the real question will become once all the issues come to light). So be it. -
SexyBeast over 9 yearsHelluva answer! Thank you!
-
SexyBeast over 9 yearsSo, for the second case, if I have a
const
objectobj
of classX
, and I callbar()
likeobj.bar(obj)
, what is supposed to happen and why? Shouldn'tobj._x = 42
fail, sinceobj
is declaredconst
in the caller? -
Gabriel Staples almost 7 yearsWhat about the case where you make the latter bar function (
void bar(X const& obj) {...}
) look like this?void bar(const X& obj) {...}
, does moving theconst
keyword to this location change anything? If so, can you add this example too please? -
Andreas Flöjt over 6 years@GabrielStaples They're the same;
const
applies to what is on its left, or to what is on its right in case there is nothing to the left. In your case you'll see that for both versionsconst
is applied toX
. -
csguy almost 5 yearsi think its slightly misleading to say "you can invoke the method on const objects of the class type" because you can invoke const method on const objects or non const objects, while non-const functions can only be called by non-const objects. otherwise this is my favorite answer
-
Leo over 2 yearsSo nice answer, easy to understand