在头文件中声明 extern const 或仅仅声明 extern 是一样的吗?也都会给外部链接吗?
全局变量
#include <string>
extern const std::string foo = "bar";
全局变量
#ifndef GLOBALS_H
#define GLOBALS_H
#include <iostream>
extern const std::string foo;
#endif /* GLOBALS_H */
或
全局变量
#ifndef GLOBALS_H
#define GLOBALS_H
#include <iostream>
extern std::string foo;
#endif /* GLOBALS_H */
两者都编译和运行良好,在多个文件中使用时都给出相同的地址,哪个更正确?
最佳答案
这个是对的,
//globals.h
extern const std::string foo; //Consistent
一致!
关于c++ - 全局外部常量说明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4547147/