Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > static member variables

Reply
Thread Tools

static member variables

 
 
John Ratliff
Guest
Posts: n/a
 
      10-13-2005
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
 
Reply With Quote
 
 
 
 
AnonMail2005@gmail.com
Guest
Posts: n/a
 
      10-13-2005
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];
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Put variables into member variables or function variables? tjumail@gmail.com C++ 9 03-23-2008 04:03 PM
Can a static member function access non-static member? dolphin C++ 3 12-05-2007 12:39 PM
Can Derived class static member access protected member from base class? Siemel Naran C++ 4 01-12-2005 06:46 PM
How would I use qsort to sort a struct with a char* member and a long member - I want to sort in order of the long member Angus Comber C Programming 7 02-05-2004 06:41 PM
performance of static member function vs. instance member function 0to60 C++ 4 11-21-2003 05:25 PM



Advertisments