[英]C++ - what's the best way to initialize a variable in one function/file and then use it in main()/another file?
In C++, let's say I need to assign something to a variable and I want to do it outside of main() so the code is clearer, but then I want to use that variable for some operations inside main() or another function. 假设在C ++中,我需要为变量分配某些内容,并且想要在main()之外进行操作,以便使代码更清晰,但是我想将该变量用于main()或其他函数中的某些操作。 For example I have:
例如,我有:
int main()
{
int a = 10;
int b = 20;
SomeFunction(a,b);
}
And I want to have something like this: 我想要这样的东西:
void Init()
{
int a = 10;
int b = 20;
}
int main()
{
SomeFunction(a,b);
}
But obviously the compiler would say a and b are undeclared in the scope of main(). 但是显然编译器会说a和b在main()范围内未声明。 I could always declare them as global variables but there probably is a better way to solve that problem and I read that global variables are not that great in the long run.
我总是可以将它们声明为全局变量,但是可能有解决该问题的更好方法,而且我读到,从长远来看,全局变量并不是那么好。 I don't want to use classes.
我不想使用类。 So what do you guys propose?
你们有什么建议?
Use structures: 使用结构:
struct data
{
int x;
int y;
};
data Init()
{
data ret;
ret.x = 2;
ret.y = 5;
return ret;
}
int main()
{
data v = Init();
SomeFunction(v.x, v.y); //or change the function and pass there the structure
return 0;
}
If you don't want to use even struct then you can pass the values to Init function by reference. 如果您不想使用偶数结构,则可以通过引用将值传递给Init函数。 But in my opinion the first version is better.
但我认为第一个版本更好。
void Init(int &a, int &b)
{
a = 5;
b = 6;
}
int main()
{
int a, b;
Init(a, b);
return 0;
}
You can use the extern
keyword. 您可以使用
extern
关键字。 It allows variables to be defined once and then used everywhere. 它允许变量定义一次,然后在任何地方使用。 You can use it like this:
您可以像这样使用它:
// main.cpp
extern int a;
extern int b;
and in your other file do 并在您的其他文件中
// Other.cpp
int a = 10;
int b = 20;
You can declare these with extern
as many times as you want, but you can only define them once. 您可以根据需要多次使用
extern
声明它们,但是只能定义一次。
Depending on the circumstances, you might want to store the values of those variables in a file and read them from the disk when you need them. 根据具体情况,您可能需要将这些变量的值存储在文件中,并在需要时从磁盘读取它们。 For example, you could have data_values.txt in which you have space separated integers: 324 26 435 ....
例如,您可能具有data_values.txt,其中您使用空格分隔的整数:324 26 435...。
Then define a file reading function in another source file, say data_reader.cpp: 然后在另一个源文件中定义一个文件读取功能,例如data_reader.cpp:
#include<fstream>
#include<string>
#include<vector>
std::vector<int> get_data(const std::string& file_name){
// Initialize a file stream
std::ifstream data_stream(file_name);
// Initialize return
std::vector<int> values;
// Read space separated integers into temp and add them to the back of
// the vector.
for (int temp; data_stream >> temp; values.push_back(temp))
{} // No loop body is necessary.
return values;
}
In whatever file you want to use that function, put 在要使用该功能的任何文件中,
#include <string>
#include <vector>
// Function declaration
std::vector<int> get_data(const std::string& file_name);
// File where the data is stored
const std::string my_data_file {"data_values.txt"};
void my_function() {
... // do stuff here
std::vector<int> data_values = get_data(my_data_file);
... // process data
}
If you are avoiding classes from the C++ standard library as well, then you would probably want to use an int array for the return value, a char* or char array for your file name and scanf or some other C function for reading the file. 如果还避免使用C ++标准库中的类,则可能要使用int数组作为返回值,将char *或char数组用作文件名,并使用scanf或其他一些C函数读取文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.