always static const if variable doesn't change?

Hey,

Let's assume i have a member Array of vectors, e.g. enemy pawn positions for a game.
So these positions never change and are always the same.

With static const the array will be created just once, and not each time the class is constructed.

For C++11 is it a good idea to declare such variables static?
Use const on variables that do not change. Use static where you want a member variable that is shared among all of the instances of that object type. The two are occasionally used together but they serve very separate purposes.

With static const the array will be created just once, and not each time the class is constructed.

Simply declaring the array as 'static' is what does this. You need the 'const' specifier because the size of the array has to be established at compile time and should not change.

... Array of vectors ...

You should rethink this approach.
Last edited on
You could use an enum, it doesn't allocate space and is a const.
it's just a example.

@Computergeek01
e.g. this array will be always the same for each object instance, so using static cosnt is totally fine?

does the compiler declare variables static anyway if they never change over all instances(optimization)?
Yes. You can take the address of a static.

You can't take the address of an enum as no memory is reserved for it, it's equivalent to a #define in some sense.
Topic archived. No new replies allowed.