开发者

Why do static variables need to be declared twice in C++

I have a header called filepaths.h which defines a number of static variables:

#ifndef FILEPATHS_H
#define FILEPATHS_H

class FilePaths {

public:

    static QString dataFolder();
    static QString profileFolder();

private:

    static QString dataFolder_;
    static QString profileFolder_;

};

}
#endif // FILEPATHS_H

And I have an associated filepaths.cpp which initially looked like this:

#include "FilePaths.h"

QString FilePaths::dataFolder() {
    return dataFolder_;
}

QString FilePaths::profileFolder() {
    return profileFolder_;
}

However that didn't work - I got an "unresolved symbol error" linker error on all the static variables. So I've added these variables to the C++ file in this way:

#include "FilePaths.h"

QString FilePaths::dataFolder_ = "";
QString FilePaths::profileFolder_ = "";

QString FilePaths::dataFolder() {
    return dataFolder_开发者_StackOverflow中文版;
}

QString FilePaths::profileFolder() {
    return profileFolder_;
}

And this works, however I don't understand why.

Why do these static variables need to be defined twice? Or maybe I'm not defining them but initializing them? But still why does it need to be done? Or should I write my class differently?


One is a definition, the other is a declaration. The difference is that declarations can appear multiple times, and for variables not in a class, maybe never at all, whereas definitions can appear once and only once.

The reasons for needing separate declarations and definitions is archaic history, the kind of thing where it basically doesn't have to be that way at all but it is that way so that C++ is compatible with C, which was designed to be compiled in the 1970s.


From http://weblogs.asp.net/whaggard/archive/2004/11/05/252685.aspx:

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.

Because, like DeadMG said, you can declare a variable many times but define it only once. I think it's like function prototypes: you can have as many of those as you want, but only one can go with a body and actually define the function.


You don't declare them twice, the declaration happens in the class header and the definition - the point where the variable is actually there and will allocate some memory - is in the .cpp part.

But the difference to a common instance variable is there, the static part is only there once per class for any instance you create.


This is because,when ever you declare a class then,you are declaring a structure for the specific instances of that class,BUT in case of static variables in a class,they are the one which can be initialized before any object of the class is created. SEE there is no space reserved when we declare a class in the memory,but the space is reserved when we declare the object of the class.NO member of the class can be initiated like int a=2; but this can be done like 'static int a=2;' is possible in the class declaration reserve space for them on there second declaration,& must be made aware of it

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜