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