Why static variables should be declared twice in C ++

I have a header called filepaths.h that determines the 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 paths.cpp file that initially looked like this:

 #include "FilePaths.h" QString FilePaths::dataFolder() { return dataFolder_; } QString FilePaths::profileFolder() { return profileFolder_; } 

However, this did not work - I received a "unresolved symbolic error" linker error for all static variables. Therefore, I added these variables to the C ++ file as follows:

 #include "FilePaths.h" QString FilePaths::dataFolder_ = ""; QString FilePaths::profileFolder_ = ""; QString FilePaths::dataFolder() { return dataFolder_; } QString FilePaths::profileFolder() { return profileFolder_; } 

And it works, but I don’t understand why.

Why do these static variables need to be defined twice? Or maybe I do not define them, but initialize them? But why should this be done? Or should I write my class differently?

+7
source share
4 answers

One is a definition, the other is an announcement. The difference is that declarations can appear several times, and for variables that are not part of the class, they may never exist at all, while definitions can appear once and only once.

The reasons for the need to use separate declarations and definitions is an archaic story, such a thing in which it should not be such, in principle, but it is so that C ++ is compatible with C, which was developed for compiled in the 1970s.

+8
source

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

You need to declare it outside the class, because otherwise the compiler does not know which translation unit (therefore, the file object) the member should accept.

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

+2
source

You do not declare them twice, the declaration occurs in the class header, and the definition - the point where the variable actually exists and allocates some memory - is in the .cpp part.

But whether there is a difference with a common instance variable, the static part is present only once for each class for any instance you create.

0
source

This is because when you declare a class, you declare a structure for specific instances of this class, BUT in the case of static variables in the class, they are those that can be initialized before any object is created. SEE does not have space reserved when we declare a class in memory, but space is reserved when we declare a class object. A member of the NOO class can be initiated as int a = 2; but it can be done as "static int a = 2;" it is possible to reserve a place for them on the second declaration in the class declaration, & should be aware of this

0
source

All Articles