What is meant with const at end of function declaration? [duplicate]
A "const function", denoted with the keyword const
after a function declaration, makes it a compiler error for this class function to change a member variable of the class. However, reading of a class variables is okay inside of the function, but writing inside of this function will generate a compiler error.
Another way of thinking about such "const function" is by viewing a class function as a normal function taking an implicit this
pointer. So a method int Foo::Bar(int random_arg)
(without the const at the end) results in a function like int Foo_Bar(Foo* this, int random_arg)
, and a call such as Foo f; f.Bar(4)
will internally correspond to something like Foo f; Foo_Bar(&f, 4)
. Now adding the const at the end (int Foo::Bar(int random_arg) const
) can then be understood as a declaration with a const this pointer: int Foo_Bar(const Foo* this, int random_arg)
. Since the type of this
in such case is const, no modifications of member variables are possible.
It is possible to loosen the "const function" restriction of not allowing the function to write to any variable of a class. To allow some of the variables to be writable even when the function is marked as a "const function", these class variables are marked with the keyword mutable
. Thus, if a class variable is marked as mutable, and a "const function" writes to this variable then the code will compile cleanly and the variable is possible to change. (C++11)
As usual when dealing with the const
keyword, changing the location of the const key word in a C++ statement has entirely different meanings. The above usage of const
only applies when adding const
to the end of the function declaration after the parenthesis.
const
is a highly overused qualifier in C++: the syntax and ordering is often not straightforward in combination with pointers. Some readings about const
correctness and the const
keyword:
Const correctness
The C++ 'const' Declaration: Why & How
What is the meaning of a const at end of a member function? [duplicate]
It means that *this
is const
inside that member function, i.e. it doesn't alter the object.
The keyword
this
is a prvalue expression whose value is the address of the object for which the function is called. The type ofthis
in a member function of a classX
isX*
. If the member function is declaredconst
, the type ofthis
isconst X*
. [section 9.3.2 §1]In a
const
member function, the object for which the function is called is accessed through aconst
access path; therefore, aconst
member function shall not modify the object and its non-static data members. [section 9.3.2 §2]
This means that a const
member function can be called on a const
instance of the class. A non-const
member function can't be called on [1]a const
object, since it could potentially try to modify it.
[1] Note: a temporary is not a const
object unless it's of const
type.
When to use const in the beginning and in the end of a method declaration in C++ [duplicate]
Your first function operates on a const
this
pointer (that is; a const object that it can't change (or at least shouldn't)).
Your second function returns a constant integer - which is somewhat nonsensical since you can just assign it to a non-const variable and change it anyway. Besides, why does the function care if you change a POD type or not?
Your third function is just a combination of the first two. A function operating on a const object returning a const value.
Const at the end of function declaration in C++ [duplicate]
It means the method is a "const method" A call to such a method cannot change any of the instance's data (with the exception of mutable
data members) and can only call other const methods.
Const methods can be called on const or non-const instances, but non-const methods can only be called on non-const instances.
struct Foo {
void bar() const {}
void boo() {}
};
Foo f0;
f0.bar(); // OK
fo.boo(); // OK
const Foo f1;
f1.bar(); // OK
f1.boo(); // Error!
why put a const at the end? [duplicate]
The const
keyword signifies that the method isn't going to change the object. Since operator==
is for comparison, nothing needs to be changed. Therefore, it is const
. It has to be omitted for methods like operator=
that DO modify the object.
It lets the compiler double-check your work to make sure you're not doing anything you're not supposed to be doing. For more information check out http://www.parashift.com/c++-faq-lite/const-correctness.html.
What does const mean following a function/method signature? [duplicate]
It means that the method do not modify member variables (except for the members declared as mutable
), so it can be called on constant instances of the class.
class A
{
public:
int foo() { return 42; }
int bar() const { return 42; }
};
void test(const A& a)
{
// Will fail
a.foo();
// Will work
a.bar();
}
const best practice in function declaration
Assuming that you are talking about member functions (non member functions cannot be const
ever), you should write const-correct code always, and that means that each and every function that does not modify the visible state of the object should be const
.
Even if you don't ever create a constant object of the type, in many cases you will pass the object to a function through const references and in that case, only const
member functions can be called on the object (Incidentally this means that you should pass the object by constant reference to any function that does not need to change the state of the received object)
Why use the keyword 'const' twice in a class member function C++
The first const
qualifies the returned value as non-modifiable; the second const
specifies that the function does not modify the class instance (or any member of it) on which it was called.
The first is more often used when the function returns a reference to a member, to prevent that returned reference from being used to modify the member to which it refers.
Related Topics
Writing a Sequence of Numbers Like: 1 22 333 4444 55555
Combining Multiple for Loops into Single Iterator
How to Get a Stack Trace for C++ Using Gcc With Line Number Information
How to Read and Parse CSV Files in C++
What Are C++ Functors and Their Uses
Difference Between Public, Private, and Protected Inheritance in C++
Why Do Function Pointer Definitions Work With Any Number of Ampersands '&' or Asterisks '*'
Calling C++ Member Functions Via a Function Pointer
How to Read in User Entered Comma Separated Integers
How to Best Silence a Warning About Unused Variables
What Is a Smart Pointer and When Should I Use One
Is Segmentation Fault Actual Undefined Behavior When We Refer to a Non-Static Data-Member