How can I use a string to create a class object?

I wanted to create a new class object and I want to name it from a string. Something like this:
1
2
string name = "Mike";
Customers name;  //to create a new object in customer class using the name string 

Is there any way to do this?
Last edited on
Use constructors:
1
2
3
4
5
6
7
8
9
10
11
12
13
class Customers
{
    std::string str;
public:
    Customers(const std::string& s) : str(s) {}
}

int main()
{
    std::string name = "Mike";
    Customers m(name);
    Customers j("John");
}
^ Or arguments to be specific.
Sorry, I still don't understand. What exactly is it that the constructor does? Is it the "m(name)" part?
Last edited on
m is an object of class Customers, created with variable name as parameter (as you can see it is used to initialize some internals.)

j is an object of class Customers, created with string literal "John" as parameter.

http://www.learncpp.com/cpp-tutorial/85-constructors/
http://www.cplusplus.com/doc/tutorial/classes/
Thanks for the reply.

What I want to do is to take the string and make it the name of the object. For example if I had a program where the user inputs a customer's name and I then need to make a class object that is named the same name the user input. In the top example, instead of creating m or j as an object I need to create the text in the string as an object.
Why do you need that? How do you intend to refer to this object later if you do not know its actual name?
The customer example was just a simple way to show what I'm talking about, but what I'm working on is a program that takes in a bunch of files and I want to store info from each file into it's own class object, so that later on I can display it in my program. When a new file is input, the program adds the names of them to a listbox. When I select something in the listbox I want it to look in the class object that was made using name of the file in order to load the details of the specific file.
Last edited on
Store a filename alongside your classes.

Like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct file_content
{
    std::string file;
    Customers customers;
};

//...
std::vector<file_content> data;

std::string filename = ask_user_for_string();
file_content temp;
temp.file = filename;
temp.customers = load_from_file(filename);
data.push_back(temp);
Or use standard std::pair to avoid defining own structure.
I would recommend a std::map<std::string, blah>
For the struct example, how would I know which data to get when a new item is selected in the listbox?

Is there somehow a way to rename a class object after it has been declared?
Last edited on
Something along these lines:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
struct info
{
    explicit info( std::string file_name )
    {
        // open file
        // read and store into member variables

    }

    // ...

    static std::map< std::string, info > look_up_table ;

    static info info_by_name( std::string file_name )
    {
        auto iter = look_up_table.find(file_name) ;

        if( iter == look_up_table.end() ) // not found; info was not loaded
            iter = look_up_table.insert( { file_name, info(file_name) } ).first ; // create info, add to look up table

        return iter->second ; // return info
    }
};
Topic archived. No new replies allowed.