Search code examples
c++global-variablesconstantsextern

global extern const clarification


Is declaring extern const or just extern the same thing in a header file? Also will both give external linkage?

globals.cpp

#include <string>

extern const std::string foo = "bar";

globals.h

#ifndef GLOBALS_H
#define GLOBALS_H

#include <iostream>

extern const std::string foo;

#endif  /* GLOBALS_H */

OR

globals.h

#ifndef GLOBALS_H
#define GLOBALS_H

#include <iostream>

extern std::string foo;

#endif  /* GLOBALS_H */

Both compiles and run fine, both give same address when used in multiple files, which one is more correct?


Solution

  • This one is correct,

    //globals.h
    
    extern const std::string foo; //Consistent
    

    Consistent!