英文:
Visible variables in c++ and how to make variable visible more
问题
我仍然是C++的初学者,但我了解一些。我正在学习第一学期,我想要制作自己的项目,我认为这是学习编程的最佳方式。无论如何,我想要从文件加载数据到动态数组(我知道如何做),但我希望这项工作由特殊函数完成,并且该数组对其他函数可见(或者是全局的)。我知道使用全局变量不是一个好主意,所以我在思考是否可能使变量与没有类的情况下成为友元(因为我还没有使用或学习类)。
提前感谢!
英文:
<br>I am still a beginner in c++, but I know something. I am studying the 1st term and I wanna make my own project, IMO it's the best way to learn to program. Anyway<br> I wanna load data from file to dynamic array (and I know how to do that) but I to that job be done by special function and to that array be visible for other function (alternativity global). I know that using global variables is not good idea so I am thinking if it's possible to make variable friend with NO classes (bc I didn't use and learn classes yet)<br>
Thanks in advance!
答案1
得分: 0
"friend"不是你要找的东西。变量只是一个有名称的对象。你在这里想要做的不是从外部访问函数的变量(实际上是不可能的,函数变量只在函数执行时存在)。你想要将对象从一个函数传递到另一个函数。这通过函数的返回值来完成:
std::vector<int> readDataFromFile() {
std::vector<int> data;
// 从文件中读取数据并存储到`data`中
return data;
}
int main() {
std::vector<int> myData = readDataFromFile();
// 根据需要使用`myData`
}
你可以看到,readDataFromFile
在其data
变量上操作,然后将其返回。这意味着,就在readDataFromFile
结束时,主函数main
中的myData
(另一个独立的对象)从data
初始化,并且数据本身也保留下来。
注意:
- 不要使用C风格的数组、
new
或delete
。这些是为了与C兼容和低级内存管理而设计的,不适合一般用途。C++中的动态数组是std::vector<YourType>
。
进一步的概念:
-
这里
myData
是移动初始化的,这意味着不会复制数据:动态数组直接从data
转移到myData
。 -
这是可以发生NRVO的情况。这是一种优化,它注意到
data
是多余的,并将其替换为对myData
的直接访问,因此在整个程序执行期间只会有一个向量对象。在一般情况下,这是不可观察的,所以你不需要担心它。
英文:
friend
is not what you're looking for. A variable is just a named object. What you want to do here is not to somehow access the function's variable from the outside (that's not actually possible, function variables only exist when the function is executing). You want to transfer the object from one function to th other. That's done through the function's return value:
std::vector<int> readDataFromFile() {
std::vector<int> data;
// Read the file and store it into `data`
return data;
}
int main() {
std::vector<int> myData = readDataFromFile();
// Use `myData` as needed
}
You can see above that readDataFromFile
works on its data
variable, then returns it. This means that, right as readDataFromFile
ends, myData
in main (another, independent object) is initialized from data
, and the data itself lives on.
Notes:
- Do not use C-style arrays,
new
ordelete
. These are meant for compatibility with C and low-level memory management, not general use. A C++ dynamic array is anstd::vector<YourType>
.
Further notions:
-
Here
myData
is move-initialized, which means that no copy of the data is made: the dynamic array is transferred directly fromdata
tomyData
-
This is a case where NRVO can occur. That's an optimization which notices that
data
is redundant, and will replace it with direct access tomyData
, so there will only ever be one vector object throughout the program's execution. This is not observable in the general case., wo you don't need to worry about it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论