471,066 Members | 1,234 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,066 software developers and data experts.

static member variables

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
Oct 13 '05 #1
1 1955
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 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by BCC | last post: by
29 posts views Thread by Alexander Mahr | last post: by
2 posts views Thread by katekukku | last post: by
25 posts views Thread by Sahil Malik [MVP] | last post: by
3 posts views Thread by Colin Desmond | last post: by
1 post views Thread by mangalalei | last post: by
5 posts views Thread by mast2as | last post: by
6 posts views Thread by Olumide | last post: by
17 posts views Thread by Juha Nieminen | last post: by
4 posts views Thread by aaragon | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.