Can you create an instance of a class using a variable?

I am trying to make a class that is created based on user input. I was wondering if it is possible, and how to, create an instance named with a variable that will change based on user input.
Something like this:

#include <iostream>
#include <string>
#include "POI.h"

std::string create_file;

int main() {
std::cout << "Name the File You Would Like to Create: ";
std::cin >> create_file;
POI create_file;
create_file.poi_add(create_file, 1, 12, 7, "unknown");
std::cout << create_file.poi_name();
}
No, it's not possible. However:
1
2
3
4
std::map<std::string, POI> variables;
std::string variable_name = user_input();
variables[variable_name] = POI();
variables[variable_name].poi_add(variables[variable_name], 1, 12, 7, "unknown");
Last edited on
create an instance named with a variable that will change based on user input

Why do you think that instances should be "named"?
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>
#include "POI.h"

int main() {
  std::string filename;
  std::cout << "Name the File You Would Like to Create: ";
  std::cin >> filename;
  POI instance;
  instance.poi_add(filename, 1, 12, 7, "unknown");
  std::cout << instance.poi_name();
}

In that it looks like a POI has an attribute name that you get with poi_name().
Last edited on
Friendly reminder to be nicer to new posters. Yes, double-posting is ill-advised, but most learn after the first time. We can show more restraint before applying the banhammer.
Topic archived. No new replies allowed.