Board logo

标题: C++ static variables [打印本页]

作者: caopengly    时间: 2008-10-15 15:02     标题: C++ static variables

I'm working on a project in C++ and I created a static member function and a static data member in one of my classes. When I tried to compile I kept getting an unresolved external error that was pointing to my static data member and I couldn't figure out what the problem was. Finally after some research (via Google, Msdn, and Stroustrup) I figured out that you have to define the static data member outside the class declaration.

I don't consider myself to be a novice C++ programmer but I don't think I ever knew this rule. Am I the only one who didn't know this? I guess for the most part I don't use static member variables very much, I know I have used local function static variables and static functions but I guess I have never used a static data member because I know I would have remembered this rule. At any rate I know now and knowing is half the battle ;)

Here is a small code snippet to show what needs to be done:

1 // MyClass.h
2 class MyClass
3 {
4 public:
5 static int GetCount() { return count; }
6 private:
7 static int count;
8 };

1 // MyClass.cpp
2 #include "MyClass.h"
3 int MyClass::count; // Also do any initialization if needed.Published Friday, November 05, 2004 12:51 AM by puzzlehacker

Comments
# re: C++ static variables
You need to declare it outside the class because otherwise the compiler doesn't know which translation unit (hence object file) the member is supposed to go.

One of the drawbacks of C++'s rather archaic compilation model.

Friday, November 05, 2004 1:39 AM by Dean Harding
# re: C++ static variables
I never knew that either. I use static variables and functions every day in C, but of course I never do C++ anymore. I'll have to remember this one.

Friday, November 05, 2004 9:07 AM by Joe Payne
# re: C++ static variables
That has always bugged me. I get around it with something like this:
static int GetCount()
{
static int count = 0;
return count;
}

This also helps to get around some "order of initialization" issues across statics. i.e. if one static initialization depends on another static.

Friday, November 05, 2004 11:32 AM by Dave Montgomery
# re: C++ static variables
Right. I was exactly searching after this. Because: hat the same problem. And I called me already a C++ expert. But you never run out of gotchas with c++. Or who knows about the explicit keyword

Monday, August 07, 2006 7:00 PM by Rob
# re: C++ static variables
It gives a linking error and you keep guessing why this is not working.You have to make the static variable declared outside the class.

Wednesday, August 16, 2006 1:55 AM by Nikhil
# re: C++ static variables
Here's an interesting question then: At what point will those static variables' memory get cleaned up? When the process exits?

Tuesday, August 22, 2006 2:28 PM by Marco Casalaina
# re: C++ static variables
put a breakpoint in a destructor and see

Friday, August 25, 2006 1:01 AM by aadad
# re: C++ static variables
I totally forgot about this rule. I spent a half day trying to rid my program of the 'unresolved external ...' errors. Thanks for the posting.






欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) Powered by Discuz! 7.0.0