Static class member variables and functions

Started by
9 comments, last by diddly-yo 21 years, 5 months ago
After looking everywhere, I''m hoping you guys can help me with a problem I''ve been having. I''ll just post some code.

class Foo
{
    static void Method1();
    static int diddly;
}

void Foo::Method1()
{
    diddly = 5;
}
 
This results in a link error: unresolved external symbol "public: static int Foo::diddly" (?diddly@Foo@@2HA) But I thought static methods were able to access static member variables? What am I doing wrong? Thanks for the help diddly
Advertisement
Try adding the line
int Foo::diddly = 0; 

somewhere in the source. Does that help at all?

Peace,
Brendon
My stuff.Shameless promotion: FreePop: The GPL god-sim.
Where do you mean? I''ve tried accessing it with Foo::diddly before, and it doesn''t work.

diddly
In the source file along with the method definitions. Just put it in as if you were declaring a global variable, but with a special name that puts it in the class: ie Foo::diddly

I hope that made more sense,
Doc
My stuff.Shameless promotion: FreePop: The GPL god-sim.
The reason for this is that for each static variable, there has to be exactly one implementation. So the implementation can''t be in the header file, since it''s included multiple times.

Don''t listen to me. I''ve had too much coffee.
quote:Original post by Sneftel
The reason for this is that for each static variable, there has to be exactly one implementation. So the implementation can''t be in the header file, since it''s included multiple times.

Agreed. But do you know why the compiler has no problem with static variables inside inlined functions?

  struct A{	void f(){		static int i;		i = 3;	}};  

Cédric
Inside inlined functions or any function whatsoever. This is because the compiler treats that variable as a global variable with scope limited to that function. For example:

void foo()
{
int A;
static int B;
}

A will be created on the stack of the function.
B will be created in the global space of the program. No matter when you enter this function, B will retain its value, unlike A which is destroyed whenever you leave the scop of foo().
People fear what they don''t understand, hate what they can''t conquer!
Umm... On second thought, there is one instance of the static variable (inside the inlined function) / object, so it must be allocated at the same time as the object, right?

Cédric

[edited by - cedricl on November 7, 2002 8:57:57 PM]
Sorry, I think I was unclear.

in foo.h
class Foo{        static void Method1();        static int diddly;} 


in foo.cpp
void Foo::Method1(){    diddly = 5;} 


Or did I miss what you were saying entirely? I thought that static member functions could access static member variables. I don''t understand why it''s giving me that error message. Why a link error? Thanks for all the help so far.

diddly
You missed what they were saying . No worry though, static class members and variables are a hairy topic.

I believe this is what you are trying to do:

  class Foo{public:	static void Method1();	static int diddly;};int Foo::diddly;void Foo::Method1(){	diddly = 5;}int main() {return 0; }  

Which compiles with no warnings/errors. The reason you have to stick that "int Foo::diddly;" in there is that static class members must be initialized only once, so you have to declare them inside of a translation unit (ie. a source file, not a header file) so the compiler knows where to look to get the data for that static object.

This topic is closed to new replies.

Advertisement