使用其他原文件的全局变量

使用其他原文件的全局变量

🔹前言:由于对全局变量的使用不当,经常容易造成一些编译错误,如:重复定义multiple definition of xxx。决定更新此篇博客,总结一下错误


普通全局变量

//hello.cpp

string ss = "hello";	//定义
//main.cpp

extern string ss;	//外部引用
int main()
{
    cout << ss << endl;	// 可正常使用
}

const全局变量

//hello.cpp

extern const string ss = "hello";	//声明+定义
//main.cpp

extern const string ss;	//外部引用
int main()
{
	cout << ss << endl;	
}

其他

  • static修饰的全局变量不能在其他原文件中访问。静态链接属性,意味着该变量的作用域只限于定义它的源文件

  • .h中定义全局变量,可能会导致重复定义错误。头文件通常会被多个源文件包含(通过#include指令),并且每个源文件都会独立进行编译


使用demo

// global.h 头文件
#pragram once

extern string globalVariable; // 全局变量的声明
// global.cpp 源文件
#include "global.h"

string globalVariable = "hello"; // 全局变量的定义
// other.cpp 源文件
#include "global.h"

extern string globalVariable; // 外部引用

// 可以在这里使用 globalVariable 了

🔹🔷🔹分割线,新问题🔹🔷🔹

使用const替换#defines

《Effective C++》的02条款中有提及更多细节,下文主要是在实际使用过程中产生的问题。


通常对全局变量的使用,即使是const修饰

//test.h
#include <iostream>
using namespace std;

extern const int var;		//声明
void run();
//test.cpp
#include "test.h"
extern const int var = 100;	//定义

void run()
{
	printf("run var=%d &var:%p\n", var, &var);
}
//main.cpp
#include "test.h"

int main()
{
    cout << "======" << endl;
    run();
    cout << "======" << endl;
    printf("main var=%d &var:%p\n", var, &var);
    return 0;
}

在这里插入图片描述

对于全局变量的使用,都比较符合预期以及前文的示例


在数组声明的场景下:

使用const全局变量Size来替换原先的宏SIZE

//test.h
#include <iostream>
using namespace std;

//#define SIZE 100
extern const int Size;		//用const替换,声明
//test.cpp
extern const int Size = 100;	//定义
//main.cpp
#include 
int main()
{
    //int a[SIZE];
    int a[Size];	//error
    
    return 0;
}

在这里插入图片描述
编译错误

原因:
在这里插入图片描述

对于数组a其类型为int [100],然后在编译时,在main.cpp中只能获取到Size的声明,无法得到其定义以及具体的值。


如果更改为在test.h文件中直接定义呢?

//test.h
#include <iostream>
using namespace std;

//#define SIZE 100
const int Size = 100;		//定义
//main.cpp
#include 
int main()
{
    //int a[SIZE];
    int a[Size];	//success
    return 0;
}

但是,如果在test.cpp中也会使用到Size呢?

//test.cpp
#include "test.h"

void run()
{
	int b[Size];
}
//main.cpp
#include 
int main()
{
    run();
    //int a[SIZE];
    int a[Size];	//success
    return 0;
}

编译,运行都成功了,好耶,🎇🎇


额,似乎是万事大吉,但是,难道不会觉得有些奇怪吗,在.h定义的全局变量,被两个.cpp都引用了,为什么没有发生多重定义的链接错误?

//test.cpp
#include "test.h"

void run()
{
	int b[Size];
	printf("run Size=%d &Size:%p\n", Size, &Size);
}
//main.cpp
#include "test.h"
int main()
{
    run();
    //int a[SIZE];
    int a[Size];
    printf("main Size=%d &Size:%p\n", Size, &Size);
    return 0;
}

在这里插入图片描述

根据运行结果,可以窥探一二,对于两个.cpp而言,他们看到的不是同一份全局变量Size,编译器对const修饰的全局变量进行了处理。其实也可以理解,因为const具有常属性,只能进行访问,即使在不同的编译单元进行使用,也不会因为修改而产生一致性问题。


推荐博文👍:对于const常量定义在头文件的思考

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值