Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 445
laurent Avatar asked Aug 06 '11 15:08

laurent


People also ask

Can static variables be declared twice?

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.

What happens if you declare a variable twice in C?

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.

How many times any static variable can be declared?

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.

Can static variables be initialized more than once?

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.


2 Answers

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.

like image 174
Puppy Avatar answered Sep 28 '22 09:09

Puppy


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.

like image 21
Seth Carnegie Avatar answered Sep 28 '22 08:09

Seth Carnegie