question about constants in C++

From: john smith (princetonharvard_at_charter.net)
Date: 11/25/04


Date: Wed, 24 Nov 2004 18:16:10 -0800

I have a question about the best way to use constants in C++. There seem to
be variations in the way that they are used, and I am confused as to what I
should be using. Basically since the constant is used in only one class,
should I try to scope it to just that class? to the file?
what are the thoughts out there.

1.)

I have seen people declare in their .h file

static const int myVariable;

And then in their .cpp put the definition.

myVariable = 10;

2.) Is it appropriate if the variable is only to be used in a class to put
that definition in the class so as to scope it to that class only?
umm, but then should it be private with an accessor?

So for example,

class MyClass {

public:
static const int myVariable = 10;

private:

}

3.)

Is it best to put it in a blank namespace in the .cpp file to keep the scope
to that file only?

So for example,

namespace {

static const int myVariable = 10;

}

I appreciate comments on this matter.
thanks for your help.



Relevant Pages

  • static enumeration alternative
    ... within the private section of my class ... static const int TRUNC_RIGHT; ... initializing them to distinct values in my .cpp file. ... irrelevant except as distinguishing markers. ...
    (comp.lang.cpp)
  • Re: Alberts MultiSelect Form Question #1
    ... This whole issue centers around what we call scope. ... are private to that ONE MODULE). ... functions and routines within that module (they default to private, ... to be used outside of that module, then I must declare them as public. ...
    (microsoft.public.access.formscoding)
  • Re: Analog & Digital scope reads of Lown wave
    ... I wrote you the private msg to avoid this thread to grow up with non ... conducted when the scope probes are attached. ... To be honest, I prefer a good glass of Lambrusco which is sweeter than ... I prefer white wines to the reds, ...
    (sci.electronics.equipment)
  • Are "privileged" methods redundant?
    ... of 'privileged' methods - 'private' methods and variables were ... available in scope of my 'public' methods. ... otherPrivateFunction(); ... " in privileged scope"); ...
    (comp.lang.javascript)
  • RE: Naming conventions for variables
    ... naming convention which guarantees avoiding Key Words as names. ... little evidence of the existence of any STANDARD naming convention in VBA -- ... SCOPE + TYPE + NAME. ... with Private scope might have a name like: ...
    (microsoft.public.excel.programming)