Archived

This topic is now archived and is closed to further replies.

carb

How to initialize protected static member vars?

Recommended Posts

The topic speaks for itself ... Let''s say I have some class:
class myClass {
protected:
   static int myVar;
}
... how do I initialize that variable in the implementation? Since the following won''t work, given that it''s protected.
int myClass::myVar = 0
Google''s my friend, but he''s not answering calls right now. Appreciate any help. - Ben

Share this post


Link to post
Share on other sites
EDIT Nevermind doesnt work

"Give a man a fish and he will eat for a day, drown a man in the water and the fish will eat for a week!

[edited by - cmptrgear on March 18, 2004 12:48:01 PM]

Share this post


Link to post
Share on other sites
carb: works for me (with borland bcc55 compiler)...

are you sure that isn't valid C++, and that is doesn't work?


[edited by - krez on March 18, 2004 1:00:27 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by carb
Let''s say I have some class:
class myClass {
protected:
static int myVar;
}

... how do I initialize that variable in the implementation? Since the following won''t work, given that it''s protected.

int myClass::myVar = 0




That is how you would do it. Works fine for me (bcc32). It should work if the compiler you are using is compliant (section 8.5 number 10 of the ISO 1998 spec seems to be the requirement). If it doesn''t work, what compiler are you using and what is the error?

TEST CODE:

#include <iostream.h>
class a
{
protected:
static int b;
public: a(){std::cout << b;}
};
int a::b = 4;
int main() { a dummy; return 0;}

...compiles with bcc32, when run prints "4".

Share this post


Link to post
Share on other sites
quote:
Original post by yy2bggggs
It should work if the compiler you are using is compliant (section 8.5 number 10 of the ISO 1998 spec seems to be the requirement).

...

#include <iostream.h>


You are not allowed to mention the C++ Standard if you use iostream.h


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan

Share this post


Link to post
Share on other sites
quote:
Original post by Fruny
quote:
Original post by yy2bggggs
It should work if the compiler you are using is compliant (section 8.5 number 10 of the ISO 1998 spec seems to be the requirement).

...

#include <iostream.h>


You are not allowed to mention the C++ Standard if you use iostream.h



17.4.1.2 requirement 4.

Share this post


Link to post
Share on other sites
Hello carb,

Maybe it is the asignment op that is given you the error.
Compiler first create your static var.
Then it asigns to it. You might not be able to in your version.

Do the following:


int myClass::myVar(0);


At creation of the static member this creates it with value you want.

Lord Bart

Share this post


Link to post
Share on other sites
quote:
Original post by Zipster
quote:
Original post by yy2bggggs
17.4.1.2 requirement 4.

What about it? It looks like you didn''t read it closely enough.



You''re right. I''ll try again. According to my latest review of the standard, 8.5 10 only says that static member initializations of this form are class scope, which doesn''t guarantee that they will conform to any access control specifiers. Section 11 deals with access control specifiers, and section 11 number 5 seems to address this point, with the following example:

quote:

class A {
typedef int I; // private member
I f();
friend I g(I);
static I x;
};

A::I A::f() { return 0; }

...

A::I A::X = 0;

Here, all the uses of A::I are well-formed because A::f and A::x are members of class A and g is a friend of class A.


This matches exactly what you are trying to do. Since this construct is deemed well-formed by the C++ standard, we can''t speak to "C++" in any meaningful way about why it doesn''t work.

Therefore, I believe this issue will not be resolved until we know the specific compiler, unless someone happens to guess the compiler (by maybe having it and being aware of this problem?) and suggests the correct resolution.

Share this post


Link to post
Share on other sites
Well, it would seem that there are certain standards the compiler doesn''t conform to perfectly, and I''m not that surprised. But I''m sure iostream.h is never standard either

Share this post


Link to post
Share on other sites
quote:
Original post by Zipster
Well, it would seem that there are certain standards the compiler doesn''t conform to perfectly, and I''m not that surprised. But I''m sure iostream.h is never standard either


I don''t understand this statement. Do you mean that bcc doesn''t conform to the 1998 standard because it has an iostream.h? The only other "the compiler" that I see that you could be referring to is the one that carb has.

Also, the term "never standard" sounds too strong to me, implying standard in any (reasonable) sense, which seems to include things like the ARM.

Share this post


Link to post
Share on other sites