static/global variable not permitted in C++

6.1k Views Asked by At

I have defined a global variable in my C++ class as follows :

std::string VAR = "HELLO_WORLD";

But cpplint is telling me :

Static/global string variables are not permitted. [runtime/string] [4]

Do you have an idea why ?

1

There are 1 best solutions below

5
On BEST ANSWER

For the avoidance of doubt the language itself permits this.

Essentially though the static analyser you are using forbids this because std::string contains a constructor so the statement actually "does something".

Therefore it needs to be inside a function, not at global scope.

On the other hand,

const char* VAR = "HELLO_WORLD";

is emitted, since that's no more than an assignment of a read-only const char[] literal to an appropriate pointer.