-2

My question title pretty much asks it all. I've recently found out that it is good programming practice in C++ to pass many values by const reference and mark certain methods in classes as a constant method. Right now, I have a library that I have been writing for myself for a while now that has absolutely no const-correctness, so I'd like to start rectifying that little by little.

In what specific scenarios should I make a method constant? So far I know "getter" methods should generally be made constant (since the code in one shouldn't modify any class variables), but do I do that for all methods that are considered getters, or only specific ones? And outside of getter methods, what other scenarios should methods be made constant?

Nkosi Dean
  • 217
  • 7
  • 18
  • Possible duplicate of [Meaning of "const" last in a C++ method declaration?](http://stackoverflow.com/questions/5598703/c-const-usage-explanation) – PRIME Dec 23 '15 at 05:35
  • 4
    You should be asking yourself when *not* to make a method `const`. – juanchopanza Dec 23 '15 at 05:35
  • 1
    @PRIME Not a duplicate. That question asks the meaning of `const` in a declaration; OP is asking _when_ it should be used. – 1201ProgramAlarm Dec 23 '15 at 05:43
  • @juanchopanza I realize that many methods need to be marked constant, but I would still appreciate, at least, a partial list of common scenarios when a method should be marked constant. I would rather not spend the time marking almost every method declaration/definition in my library constant, only to find out later half of the ones I marked as constant should not have been marked constant, for whatever reason. – Nkosi Dean Dec 23 '15 at 05:44
  • 1
    Generally use `const` when the method does not make any (observable) changes to the state of the object. The simple test of this is if you don't change anything, make the method const. – 1201ProgramAlarm Dec 23 '15 at 05:44
  • Do you know what a `const` method means? It doesn't (or shouldn't) modify the observable state of the object it is applied to. So, use if for methods that don't modify the state. – juanchopanza Dec 23 '15 at 05:46
  • @1201ProgramAlarm : the link to URL posted points to question "http://stackoverflow.com/questions/5598703/c-const-usage-explanation". Something wrong with the text of link – PRIME Dec 23 '15 at 05:48
  • @PRIME That's a different question that what I got when I clicked the link before I commented. – 1201ProgramAlarm Dec 23 '15 at 05:50
  • Okay, I marked an answer, and I believe I understand now. Because of the scope of what should be marked as constant (anything that doesn't change the values of member variables), I just wanted to play it safe and make sure there are not a lot of specific scenarios that would be exceptions to this rule. I will spend time editing my library and mark all methods as constant if they make no change to any of the member variables (or state of the member variables). – Nkosi Dean Dec 23 '15 at 05:59
  • 1
    Better rule of thumb: mark everything `const`. Then see if anything really needs to be non-const. – juanchopanza Dec 23 '15 at 06:08

2 Answers2

6

but do I do that for all methods that are considered getters, or only specific ones?

You should do that for all methods that don't modify the non-mutable members. They include not only getter functions but also any overloaded operator functions, such as operator==, operator!=, operator<.

R Sahu
  • 200,579
  • 13
  • 144
  • 260
2

When should I make a method constant?

As a rule of the thumb, you should make a method constant whenever none of the object's member variables are altered.

As an additional guideline, you should do this only when you are sure the method will not be altered in the future to change member variables.

Ultimately, if you are planning to make a method affect member variables, it should be non-constant. Otherwise, it should be constant.

Bryn McKerracher
  • 683
  • 6
  • 21
  • The usual advice is "whenever the object's state is not changed". If some of the state is held in a block of memory referenced by a pointer, then not only is changing the pointer non-const, so is changing any of the pointed-to variables. – Martin Bonner supports Monica Dec 23 '15 at 06:23