-
const on a function???
I was looking at some C++ code and I notice that a function had const at the end of it. I have ask and ried to research, but haven't gotten a good answer for why you would use const that way. Example below.
in header file
int myFunction(int &i, const int j, const int x) const;
in source file
int myFunction(int &i, cont int j, const int x) const
{
do some stuff
return val;
}
Why is there const at the end of the function and what will it gain you?
-
the const keyword at the end of a class method promises that that method won't change data members of the class. If ou try to do so after putting that keyword there, then the compiler should send an error or a warning.
-
that being the case th implementation of said function will look a bit like this:
Code:
int MyClass::myFunction(int &i, cont int j, const int x) const
{
do some stuff
return val;
}