Variable Declaration for several functions

Hello,

I'm not entirely sure if what I'm asking is possible, but here it goes:

I'm adding a function to a program written in C++. I'm trying to clean it up to make it easier to edit later, and would like to break the tasks down into sub-functions since the current script is around 5000 lines long. The function parses data from several files, manipulates the data, writes a few input files for another program, and runs the outside program with the input files. Some of the variables that are parsed are used in multiple tasks.

Now, what I'd like to do is have one function that declares all of the variables with the parsed data. I would like these variables to be visible and usable by the other functions. Keep in mind, mine is not the "main" function, and I am not at liberty to vary said function.

Is there some way to do what I've proposed?

Thanks for any help'
Ben
Err...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int foo;

void populate_foo() {

  foo = 1;

}

void do_stuff_with_foo {

  if (foo == 1) {

     // Do crazy stuff

  }

}

void main() {

  populate_foo();
  do_stuff_with_foo();

}
Last edited on
Global variables are a valid solution. But they can get annoying to keep track of in large applications. So since the OP is implying that this is a group project I would shy away from that direction.

The approach I would take is to create a struct that takes the names of the input files as arguments to the constructor, and has member functions that "manipulate the data" as you put it. This would keep each set of data in a single object and allow your subfunctions to have Global like access to the data without interfearing with anyother part of the program your collegues might write.
Thanks for the help! I've been coding for a while, but am quite new to C++, so sorry for such a novice question. I may have to invest in a book or something... :-/
Topic archived. No new replies allowed.