i'm trying to understand class getters and setters functions...
My question is: If i design a function that just only get a state from its class (a "getter" function), why mark it as "const member function"? I mean, why use a const member function if my function is designed to not change any proprieties of its class? i don't understand please :(
for example:
int GetValue() {return a_private_variable;}
and
int GetValue() const {return a_private_variable;}
what is the real difference?
Declaring a member function with the const keyword specifies that the function is a "read-only" function that doesn't modify the object for which it's called. A constant member function can't modify any non-static data members or call any member functions that aren't constant.
A function becomes const when the const keyword is used in the function's declaration. The idea of const functions is not to allow them to modify the object on which they are called. It is recommended the practice to make as many functions const as possible so that accidental changes to objects are avoided.
Declaring function parameters const indicates that the function promises not to change these values. In C, function arguments are passed by value rather than by reference. Although a function may change the values passed in, these changed values are discarded once the function returns.
Const is particularly useful with pointers or references passed to a function--it's an instantly understandable "API contract" of sorts that the function won't change the passed object.
Like member functions and member function arguments, the objects of a class can also be declared as const. an object declared as const cannot be modified and hence, can invoke only const member functions as these functions ensure not to modify the object. 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. When a function is declared as const, it can be called on any type of object, const object as well as non-const objects.
Therefore, a const member function can not return a non-const reference to a member, as that would allow the caller to have non-const access to that const member. Const member functions can return const references to members.
To make getValue () a const member function, we simply append the const keyword to the function prototype, after the parameter list, but before the function body: Now getValue () has been made a const member function, which means we can call it on any const objects.
When you declare a member function as const
, like in
int GetValue() const;
then you tell the compiler that it will not modify anything in the object.
That also means you can call the member function on constant object. If you don't have the const
modifier then you can't call it on an object that has been defined as const
. You can still call const
member functions on non-constant objects.
Also note that the const
modifier is part of the member function signature, which means you can overload it with a non-const function. That is you can have
int GetValue() const;
int GetValue();
in the same class.
const
can show up in three different places in C++.
Example 1:
const Object obj1;
const
object. Meaning that you can not change anything on this object. This object can only call const
member functions like int GetValue() const {return a_private_variable;}
Example 2:
int myMethod() const {//do something}
const
method. It would be a const
member function if it is declared inside of a class. These are the types of methods that const
variables can call.Example 3:
int myMethod(const Object &x) {//do something with x}
const
parameter. This means that the logic inside myMethod
is not allowed to change anything to do with x
. Also note the parameter is being passed by reference not by copy. I like to think of this as a read only type of method.When you are developing software that will be used by others; it is a good idea to not let them break things they don't know they should not break. In this case you can constrain variables, methods, and parameters to be const
to guaranteed that the contract is upheld. I tried to summarize the main ideas I learned in college, but there are many resources online around const
in C++. Check out this link if you would like to know more. Also it is possible that I remembered somethings incorrectly as I have not been in the C/C++ realm for a while.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With