Archived

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

Static class member variables and functions

This topic is 5515 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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().

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites