static member variables

Discussion in 'C++' started by John Ratliff, Oct 13, 2005.

  1. John Ratliff

    John Ratliff Guest

    Do I have to declare store in my implementation file for all static
    member variables, even when they are const ints?

    In Windows, using msys with g++ 3.4.2 and whatever linker I'm not sure
    (probably gnu binutils), I didn't have to declare storage for any of my
    static const integer member variables, but in Linux, the linker can't
    find four of them. I'm assuming the ones it found were simply replaced
    by the compiler and it didn't need to keep them stored. Perhaps the
    Windows g++ optimised the storage space away while my Linux g++ (3.3.6)
    didn't.

    Here is what I want to do:

    class foo {
    public:
    static const int bar = 3;
    };

    Then I might reference bar somewhere down the line. Can I initialize bar
    here in the header file? Do I still need to have

    const int foo::bar;

    in my implementation (cpp) file?

    As I said, I didn't need that const int foo::bar line in Windows under
    msys, but Linux isn't liking it. I'm assuming this is an optimization
    thing, and I probably needed storage declarations for all my static
    variables. Is this right?

    Can I still initialize my variables in the class def even if I defined
    them in the impl file?

    My big problem, and the reason I didn't declare storage space in the
    first place is I have something that looks like this:

    class foo {
    public:
    static const int SRAM_FILE_SIZE = 0x2000;
    private:
    char buffer[SRAM_FILE_SIZE];
    };

    If I don't declare the value before buffer, it can't use the constant.

    Thanks,

    --John Ratliff
     
    John Ratliff, Oct 13, 2005
    #1
    1. Advertising

  2. John Ratliff

    Guest

    According to Item #2 in Meyer's Effective C++, Third Edition,
    static const integral types are allowed to be initialized at
    the point of declaration (i.e. in the .h file).

    In some cases compilers that support this may still require
    a definition in the cpp file. For example, if you take the
    address of the variable. From that item, it is clear that
    if initialization is done in the .h file you can't do it in
    the .cpp file. But it is not clear if either or is allowed.

    Older compilers may not support this so you just need to
    define and initialize the variable in the cpp file. For
    these older compilers, where you need the const variable's
    value further down in the class declaration (e.g. for an
    array declaration) Meyer's describes the enum hack:

    class A{
    enum { Size = 100 };
    int Array[Size];
    }
     
    , Oct 13, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. 0to60
    Replies:
    4
    Views:
    518
    jeffc
    Nov 21, 2003
  2. Siemel Naran
    Replies:
    4
    Views:
    809
    Micah Cowan
    Jan 12, 2005
  3. aling
    Replies:
    6
    Views:
    484
    Xiaobin.Huang
    Oct 30, 2005
  4. dolphin
    Replies:
    3
    Views:
    1,349
    Pete Becker
    Dec 5, 2007
  5. Replies:
    9
    Views:
    961
Loading...

Share This Page