在C ++中的多个文件中使用相同的变量

编程入门 行业动态 更新时间:2024-10-27 18:32:04
本文介绍了在C ++中的多个文件中使用相同的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在更改某些代码的过程中,我已经将一些功能溢出到了多个文件中.我有文件 controls.cpp 和 display.cpp ,我希望能够访问两个文件中的同一组变量.我不介意在哪里初始化或声明它们,只要两个文件中的函数都可以使用它们.

In the process of changing some code, I have spilt some functions into multiple files. I have the files controls.cpp and display.cpp and I would like to be able to have access to the same set of variables in both files. I don't mind where they are initialized or declared, as long as the functions in both files can use them.

当函数位于同一文件中时,这不是问题,但是经过一个小时的谷歌搜索和各种尝试之后,现在看来几乎是不可能的.

This was not an issue when the functions were in the same file, but now it seems almost impossible after an hour of googling and trying various things.

推荐答案

在一个文件中定义变量,例如:

Define the variable in one file like:

type var_name;

并在另一个文件中全局声明它,例如:

And declare it global in the other file like:

extern type var_name;

更多推荐

在C ++中的多个文件中使用相同的变量

本文发布于:2023-07-26 07:03:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1214859.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   变量   文件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!