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?
Yes. Both the variable b in main and func are different although they have same name. static keyword only change the lifetime of a variable not its visibility.
It declares the identifier x but does not reserve memory for it. Both of these declarations give x external linkage. This means, when they appear in different source files, the two instances of the identifier will be linked to refer to the same thing in memory.
A static variable declaration is only executed once, the first time the function is executed. A static variable is initialized only once (since this is part of the declaration process) and will be initialized to 0 unless the programmer designates otherwise.
Static variables are initialized only once. Compiler persist the variable till the end of the program. Static variable can be defined inside or outside the function.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With