Using variable as name for a variable in a class.

I looked into some other topics on here about using a variable as a variable name by doing
1
2
3
4
for(int i=0;i<numP;i++){ // numP being the number of people
       class_name:: vector<int> variable(i);
       (all the other junk i'm doing)
     } 

This seemed to work for other people but it seems that it does not work exactly the same when used in a class. I would assume since it wants to use information from that class. Is there a way to do this inside of a class or should I search for another solution to my dynamic variable problem?
Last edited on
I have no idea what you mean.
class_name looks like a namespace or an encompassing class. Which is okay but not dynamic.

If you want to store data according to another variable you should take a look at associative containers like map:

http://www.cplusplus.com/reference/map/map/?kw=map
I do not mean that I want the class name to be dynamic, I instead want the variable name to be dynamic, I was only putting the "class_name" there to show that it was not an integer or any normal type, since it was a class I created for this assignment.
@OP,

I'm sure what you posted means something to you. Line 2 has me very confused.

Is vector<int> a template class defined within your class_name class?

Is "variable" an object or a function? It looks like an object to me, with "i" as a constructor argument. This is confusing because std::vector<> has a constructor with a single int argument which is the initial size of the vector. Since we don't know what class_name::vector<>(int) does, it looks like you are creating a unique-sized vector for each of the people in "numP".

This code snippet that you gave us to clarify what you are asking for did just the opposite. It confuses the bejeebers out of me.

This seemed to work for other people

What worked for other people? Please cut and paste specific code that works for other people so we know what we are comparing. Right now all I can read is "something used to work for somebody else...please make this, which may or may not be similar, work for me."
I instead want the variable name to be dynamic

There are no names in a final program. The names in the source code are for you.

What you cannot achieve with "static" names?
Alright, sorry for not explaining well enough. I am writing a program where I have to create a schedule for a group of people with an undisclosed number of people. I have to use a class to make a "variable" for each person. That variable being all of their information, from name to schedule. I start off the program by asking how many people there are being inputted into the system and I need a way to create new "variables" using the class I created. Sorry for not being clear, I am also kinda confused myself. The "vector<int>" part was what I saw in another post about creating dynamic naming for variables. But, because the error I got said that "vector" was not part of the Class I made, I assumed that it would not work since vector is a different class. Hopefully this makes more sense.

Also I did look into using a map, but honestly I just dont get how that works, my brain isnt quite working right now.
Last edited on
I think you're looking for something along these lines, except that you need to be able to add more information to the objects.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include <vector>

class Person {
    std::string name;
public:
    Person(const std::string& name) : name(name) { }

    friend std::ostream& operator<<(std::ostream& os, const Person& p) {
        return os << p.name;
    }
};

int main() {
    std::vector<Person> people;
    people.push_back(Person("Alvin"));
    people.push_back(Person("Bob"));
    people.push_back(Person("Chuck"));

    for (const auto& p: people)
        std::cout << p << '\n';
}

What dutch wrote.

The range-based for-loop syntax hides details (good in practise, less so while wrapping your head around concepts).

If you insist on seeing "variable names", then they are (by line 20) people[0], people[1], and people[2]. Sufficiently "dynamic"?


The push_back() has got an alternative: people.emplace_back( "Alvin" );

I start off the program by asking how many people there are being inputted

That allows small optimization:
1
2
3
4
5
std::vector<Person> people;
size_t people_to_add = 3; // pretend I did ask from user
people.reserve( people_to_add ); // sufficient preallocation
people.emplace_back( "Alvin" );
// ... 


Even if you do know the number, do not trust that user/input will give what they promise:
1
2
3
4
5
6
7
8
std::vector<Person> people;
size_t people_to_add = 3; // pretend I did ask from user
people.reserve( people_to_add );

std::string name;
while ( people.size() < people_to_add && std::cin >> name ) {
  people.emplace_back( name );
}

I decided that it would be easier on me, and the person grading, if I just went ahead and used an array. honestly I should have used that from the beginning, it would have saved a lot of time and grief.
Thanks for your inputs, they were very helpful and I may end up using them in the future, just not for this project.
Propos to you, Tikermothyn, for both having a brain and being polite. You rock!


Whenever you want to provide the user with the ability to store (key,value) pairs, that is where one of the std::map objects are useful.

But within the confines of your program, part of that is having an idea of what a person may be asked to input and how your program will work over it. Which is why the simpler solution worked better.

Even if you want to provide a user-controlled environment (variable names), your program must put some limits on it. Otherwise your users might as well be writing their own programs.

Hope this helps.
Topic archived. No new replies allowed.