alfa beta
alfa beta

Reputation: 143

Why use a const member function?

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?

Upvotes: 12

Views: 17478

Answers (3)

Noah Herron
Noah Herron

Reputation: 640

const can show up in three different places in C++.

Example 1:

const Object obj1;

  • obj1 is a 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}

  • This is a 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}

  • This is a method that takes a 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.

Upvotes: 13

Xavier Imbs
Xavier Imbs

Reputation: 212

A const instance of a class can only call const functions.

Having a const instance of a class is useful for making your programs more stable since then you can't modify the instance by accident.

In your case the functions do exactly the same thing, but it doesn't have to be that way.

Upvotes: 0

Some programmer dude
Some programmer dude

Reputation: 409136

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.

Upvotes: 39

Related Questions