Virritus
Virritus

Reputation: 39

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. 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(). 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?

Upvotes: 1

Views: 4465

Answers (3)

Jim Vargo
Jim Vargo

Reputation: 362

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 ....

Then define a file reading function in another source file, say 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.

Upvotes: 0

phantom
phantom

Reputation: 3342

You can use the extern keyword. 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.

You can read more about extern here.

Upvotes: 4

AdamF
AdamF

Reputation: 2601

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. 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;
}

Upvotes: 5

Related Questions