How can I use an array? The names are the name of some histograms I want to build and they are given by the user. For example I have this in my private:
This works fine. Now I want to add one more object to ObjectType, but this time I don't know its name. It would be given by the user and more than one can be given so I would like to pass somehow this array of entries. Something like this:
As keskiverto implied, C++ is a statically compiled language. That means you can't change code at execution time (including adding a member to an enum).
enums are simply names for numbers known by the compiler. Once your program is compiled, the name is no longer meaningful (except to the debugger).
That's doesn't mean you can't do what you want, just that it will take a little more work. I would suggest using a map.
1 2 3 4 5 6 7 8
std::map<string>, int> objectTypes;
// populate objectTypes with initial 6 names and values
// Add a user defined objectType
std::string newObjectName = "user defined name";
int newObjectValue = objectTypes.size()+1;
std::pair<string,int> pr(newObjectName, newObjectValue);
objectTypes.insert (pr);
You can populate the initial map with the six values you know. Then as the user wants to add types, you can add then to the map.
I'm not sure that it's going to be possible. There's a mismatch between compile-time and run-time processing here. The enum ObjectType is set at compile time. It can't be changed later.
I have a feeling that what you're attempting could possibly mean redesigning the entire program to operate differently, which other parts of the program would need to be changed - who knows - it could be two or three lines in one file, or hundreds of lines in many different files (source code that is).
I think I explained it a bit wrong, sorry for this. The input array is specified by the user, but it is known before hand. So when I compile the program, the array is already there, filled. It can be different every time, but it is known before the programs starts. So all I want to do is take that array and use it as I mentioned above. How can I do this?
The member variables are decided before you run the compiler. Every user will get the same binary. On every run of the program every user can give different names. One unchanged binary must work for all.
But the array is there before compilation. Like we can assume it is in the same C++ file and it is taken as in input. I just want to somehow loop over that array, that is there.
// file user_specified_object_types.h
//
// ***** add additional user specified object types in this file
// between the two lines containing only //////// *****
// do not add anything else in this file unless it is a line starting with //
//
// the object types must start with an alpha character, must not contain embedded spaces,
// and may only contain alphanumeric characters or underscores
// each object type must have a unique name, and must end in a comma ,
//
// the following object types are reserved and must not be used:
// InputDt, CheckSectors, HTrphiHis, RZfilters, BusyEvents, TrackCands
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
// ***** end additional object types **********