gpt4 book ai didi

c++ - 全局外部常量说明

转载 作者:太空狗 更新时间:2023-10-29 20:31:19 25 4
gpt4 key购买 nike

在头文件中声明 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com