I have a class with a data attribute, say of type int, which should be constant throughout the run of the program, and have the same value in all class instances. I want this value to be accessable through a public member function called get_value(). The obvious way to do this is to define a private static const class member and have get_value return it.
Alternatively, I could just place the value in the definition of get_value itself: for example, int get_value()const{return 5;}. Is the first method obviously better than the second?
From a computational point of view there shouldn’t be much differences in term of compiled code.
But with the first method your are not hiding variable inside your function : if you have not 1 but 10 of these variable I would expect to see them defined at the begin of your class, not inside some getter functions 10’s of lines below.
I consider it cleaner, I know what are “magic values” by looking at your class, I don’t have to go through your functions.