Static variables inside classes?

I have some code where in my main function I create an array of objects using a for loop. I would like to be able to access those objects outside of the loop.
It would be even better if I could access them in other functions, but I'd settle for the main function.
I really don't understand how to properly use static variable, or if that is even what I need. Any suggestions. Here is my main function.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
int main(int argc, char *argv[])
{
   if(argc < 2) {help();}                                ///Eventually there will be a help
   else{

       Argument argnum[argc];                             ///Creates Argument value objects for each arguement.
       PixelCount pixnum[argc];                           ///Creates Graphical value objects for each arguement.
       if ( DEBUGLEVEL >=1 ) cout << argc <<" arguments.\n";

       for(int i = 1; i<= argc-1; i++){                   ///Parse through each arguement, set the target,
           argnum[i].setArgument(argv[i]);                ///and evaluate the arguement to determine it's
           argnum[i].evalArgument();                      ///type.
           if (argnum[i].validateArgument() ==1){         ///If argument i is a file.
               if ( DEBUGLEVEL >=1 ) cout << "Argument " << i << "is a regular file\n";
               pixnum[i].getImage(argv[i]);               ///Load the Image B/W.
               pixnum[i].getPixData();                    ///Collect all pixel data.
               argnum[i].getSize();                       ///Get image size.
           }

           if (argnum[i].validateArgument() ==2){         ///If argument i is a directory
               argnum[i].setArgument(argv[i]);            ///Set target.
               argnum[i].openDirectory();                 ///Open the directory and feed to a path vector

        //use boost to scan the whole directory, don't forget to incorperate the job files
           }


           if (argnum[i].validateArgument() ==3){         ///If the argument is a flag,
               argnum[i].stripFlags();                    ///send to flag stripping function.

               if (argnum[i].s) outputFormatCSV=true;     ///Set output type to CSV
               if (argnum[i].h) help();                   ///Print Help
               if (argnum[i].v) version();                ///Print Version


            }


   }




   }


if (argnum[1].existing) cout << "existing\n"
    if (argc > 1 && outputFormatCSV) outputToCSV();
    if (argc > 1 && !outputFormatCSV) outputToScreen();
    else return 0;

}
You should really be more specific, I do not see much relevance of your code to the question you are asking.
Dan Feerst:
I create an array of objects using a for loop. I would like to be able to access those objects outside of the loop.

If you create an array of objects like this:
1
2
3
4
5
6
MyClass arrayItems[10] // array of ten items
for (int i = 0; i < 10; i++)
{
    // run some kind of initializing function on each object
    arrayItems[i].initObject(); 
}

You can still access the objects outside of the loop using array index, or assign them to a new variable:
1
2
3
4
5
6
7
8
9
10
11
12
// Access object inside the array
arrayItems[5].runMe(); 
// Assign objects to different variable (reference)
MyClass & myClassReference = arrayItems[5];
myClassReference.runMe();
// Pass item to a function:
void processObject(MyClass obj); // function acceps objects of MyClass
int main(){
    // ...
    processObject(arrayItems[6]);
    processObject(myClassReference);
}

Where in your code is any use of static variable? And what do you have in mind talking about static, because this keyword has six or so very different meanings in C++?
Last edited on
I'm not sure I fully understand what your code is doing, but I would say this:
I would like to be able to access those objects outside of the loop.

Then declare the objects outside the loop.
It would be even better if I could access them in other functions, but I'd settle for the main function.

Then pass the objects to those functions.
If your program was written in C++ then this declaration of the array is invalid

1
2
3
4
5
6
int main(int argc, char *argv[])
{
   if(argc < 2) {help();}                                ///Eventually there will be a help
   else{

       Argument argnum[argc]; 


Arrays shall be set by means of constant expressions specifying their sizes.

You could allocate the array dynamically using the operator new.

Topic archived. No new replies allowed.