tags:

views:

86

answers:

4

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?

+1  A: 

I think that this is just matter of style as the compile will optimise the const away in most cases.

Preet Sangha
Is one style generally preferred?
stw
I like the less is more approach these days. The less code there is, the less to read.
Preet Sangha
A: 

Why don't you just declare your static constant as public as follows?

#include <iostream>
using namespace std;

class Foo {
public:
  static const int VALUE = 5;
};

int main() {
  cout << Foo::VALUE << endl;
  return 0;
}

You could of course then add a public member get_value but I don't know what that would give you. I don't believe that this breaks encapsulation since VALUE is a constant: I would not expose a static or member variable in this way but it seems reasonable for a constant and there is some precedent for this in the C++ standard library.

Richard Cook
`public` data members reduce encapsulation. They are avoided as long as possible.
ArunSaha
The class is actually part of a hierarchy with `get_value` virtual, so I need to preserve the interface
stw
@stw: Good reason. +1. Actually, this is an important thing to add to the problem statement. If this method has to be virtual this prevents some of the more obvious compiler optimizations from being possible (inlining etc.)
Richard Cook
Would that then favour one method over the other?
stw
On second thought, probably not - though somebody who's smarter than me might be able to comment further. I suspect that the compiler would inline the integer constant into the body of the `get_value` method regardless. However, the call to `get_value` could not itself be inlined in some cases since it's virtual.
Richard Cook
+2  A: 

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.

Loïc Février
I guessed that computationally the two would be similar, so I was really asking about style. The value needs to be accessed through a public method (which a client would want to know about), and could be defined in the class body where the client can see it. It just seems to me that using a function and a static member I'm adding extra code that isn't needed.
stw
When I'm looking for constants/hard-coded variables/magic variables in code files I will look at : the top of the file / the top of classes definitions. Looking into functions definitions wouldn't be natural. It's extra code, but easier to read.
Loïc Février
+2  A: 

Think about a maintenance programmer who looks at your code a year from now and thinks: why five? What does five mean? Maybe it's really six when we work on Saturdays?

Compare his task if he sees this instead:

   int const MY_BEST_GUESS_AT_LOW_WATERMARK = 5;
   return MY_BEST_GUESS_AT_LOW_WATERMARK;
Alex Emelianov
I simplified the function name for illustration- it reality it has a more descriptive name. The comparison really is between a function and a static variable (both with descriptive names), or just the function
stw
My point stands, go for readability. Compiler will generate the same code, since your values are known at compile time. If you think your constant values are part of the *interface* of your class (i.e. your class says "this parameter defaults to 5" to the world), define them in the class body. If you consider them implementation details, subject to change without notice, hide them in the implementation file.
Alex Emelianov