Why do static variables need to be declared twice

2019-06-17 07:54发布

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_;
}

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?

4条回答
ゆ 、 Hurt°
2楼-- · 2019-06-17 08:22

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.

查看更多
【Aperson】
3楼-- · 2019-06-17 08:29

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.

查看更多
We Are One
4楼-- · 2019-06-17 08:32

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.

查看更多
女痞
5楼-- · 2019-06-17 08:41

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

查看更多
登录 后发表回答