Declare members of an array in a class

Aug 7, 2017 at 11:37am
Hello! I have an array of strings and I want to declare several variables in a class whose names are the elements of the array. Something like this:

1
2
3
4
5
6
class Class {
 private:
 for(i=0;i<=10;i++){
  int [array[i]];
 }
}


Can someone help me with this, as I haven't managed to do it. Thank you!
Aug 7, 2017 at 11:43am
What do you actually try to achieve?
Aug 7, 2017 at 11:44am
I want to declare some private members of a class, whose name to be the ones in the array, without adding them one by one
Aug 7, 2017 at 12:18pm
That is merely your means to an end. What is the end?

Why named variables? Why not an array? Who/what will use those names?

Members are defined statically. There is no code execution during compilation (unless one counts optimization and template metaprogramming).
Aug 7, 2017 at 12:30pm
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:
1
2
3
4
5
6
7
8
enum ObjectType{
    InputDt=1,
    CheckSectors=2,
    HTrphiHis=3,
    RZfilters=4,
    BusyEvents=5,
    TrackCands=6, 
  };


and later in one of the member function I do
 
dirs_.insert(std::pair< ObjectType, TFileDirectory >(InputDt, fs->mkdir("InputData") ) );

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:
1
2
3
4
5
6
7
8
9
10
11
enum ObjectType{
    InputDt=1,
    CheckSectors=2,
    HTrphiHis=3,
    RZfilters=4,
    BusyEvents=5,
    TrackCands=6, 
    for(int i=0;i<=n;i++){
    [array[i]]=6+i
   }
  };

and later:
1
2
3
for(int i=0;i<=n;i++){
dirs_.insert(std::pair< ObjectType, TFileDirectory >([array[i]], fs->mkdir((array[i]) ) );
}

I am open to any suggestion that can help me do this.

Aug 7, 2017 at 1:12pm
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.
Aug 7, 2017 at 1:21pm
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).
Aug 7, 2017 at 1:23pm
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?
Aug 7, 2017 at 1:30pm
It can be different every time

That implies you can't compile it into the program. Your best option is to read it in from a file into some type of container.
Last edited on Aug 7, 2017 at 1:30pm
Aug 7, 2017 at 1:35pm
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.

Perhaps:
1
2
3
4
5
std::vector<std::string> bar;

for( size_t i=0; i < bar.size(); ++i) {
  dirs_.insert(std::pair< size_t, TFileDirectory >( i, fs->mkdir( bar[i] ) ) );
}

The 'bar' is an array. The size and content can change during runtime.
Aug 7, 2017 at 1:42pm
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.
Aug 7, 2017 at 2:06pm
File file user_specified_object_types.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 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 ********** 


And then:
1
2
3
4
5
6
7
8
9
10
11
12
enum object_type  {

    InputDt = 1,
    CheckSectors = 2,
    HTrphiHis = 3,
    RZfilters = 4,
    BusyEvents = 5,
    TrackCands = 6,

#include "user_specified_object_types.h"

};
Aug 7, 2017 at 2:21pm
closed account (48T7M4Gy)
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
#include <iostream>
#include <string>

const int SIZE = 5;
class StringArray
{
private:
    std::string array[SIZE];
    
public:
    StringArray(){
        for(int i = 0; i < SIZE; i++){
            array[i] = "No: " + std::to_string(i);
        }
    }
    
    void setItem(std::string aString, int index){
        if( index >= 0 and  index < SIZE)
            array[index] = aString;
    }
    
    void display(){
        for(int i = 0; i < SIZE; i++){
            std::cout << array[i] << '\n';
        }
        std::cout << '\n';
    }
};

int main()
{
    StringArray a, b;
    a.display();
    
    a.setItem("3.  try this", 3);
    a.display();
    
    b.setItem("1.  another attempt", 1);
    b.setItem("2.  test",2);
    b.setItem("1.  try this", 1);
    
    b.display();
    
    return 0;
}



No: 0
No: 1
No: 2
No: 3
No: 4

No: 0
No: 1
No: 2
3.  try this
No: 4

No: 0
1.  try this
2.  test
No: 3
No: 4

Program ended with exit code: 0
Topic archived. No new replies allowed.