string conversion

Is there a way that I can use a string to create an object.
Eg. A user enters a name "Test" and then create an object of a specified type called Test.

The object is defined by a user class.

Are you asking if you can create an object of type Test at runtime, or asking if you can create an object named test at runtime?

That is, which of these is it that you're asking:

1
2
int Test; // An object named Test
Test someName; // An object of type Test 
Last edited on
Google "factory pattern."
The second option. ie the type already exists, the object will be created at run time.
What i really want to do whenever the user enters a name for a folder(say that name is Engineering) as a string that name is taken and used to create an object of type FOLDER and the name of the object is called "Engineering".


class Folder
{

}
cin >> "Engineering";

Folder Engineering;


I hope I am being clear
Not possible, and also not necessary. Why do you think you need to do this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>

struct folder
{
    explicit folder( std::string name ) : name_(name) {}

    std::string name() const { return name_ ; }

    private: std::string name_ ;
};

int main()
{
    std::cout << "name of the folder? " ;
    std::string folder_name ;
    std::cin >> folder_name ;

    folder folder_one(folder_name) ;
    std::cout << "the name of the folder is: '" << folder_one.name() << "'\n" ;
}
@xismn
It does sound that far-fetched. The idea is that i want the program to create a new object of a particular type that has a name that is specified by the user. The end result will be the number of objects created is determined by the number of names given by the user but all of the same type
It does sound that far-fetched.

Not that far-fetched. Just map a name to a specific object and you're good to go. Whenever you're presented with the name, look up the associated object.

@JLBorges
Not understanding line 6 declaration
Variable names cease to exist during the compilation and linking process. They're identifiers used by the compiler to know which of all the many objects you mean, but the names are discarded after that because there's really no use for them. If you're simply trying to keep track of which object was created when, or let the user label the objects, then you can either have the objects hold their own label (as in JLB's code above) or you can simply have a map (i.e. a look up table) that keeps track of what each object has been named (as Cire suggests).

Are you asking out of curiosity (in which case this is an interesting and educational diversion), or is there some problem you're trying to solve?
Last edited on
> Not understanding line 6

1
2
3
4
struct folder
{
    explicit folder( std::string name ) : name_(name) {}
    // ... 

Constructor folder::folder


explicit folder( std::string name ) explicit: this constructor is not a converting-constructor.
http://en.cppreference.com/w/cpp/language/explicit
http://en.cppreference.com/w/cpp/language/converting_constructor


explicit folder( std::string name ) : name_(name) {}
Initialise the non-static member _name with the string name.
: name_(name) is the mem-initializer-list, name_(name) is the lone mem-initializer
http://en.cppreference.com/w/cpp/language/initializer_list


Perhaps the use of the identifier name in more than one context (more than one scope) is the source of this doubt;
this may be a bit easier to understand:
1
2
3
4
5
6
7
8
9
10
#include <string>

struct folder
{
    explicit folder( std::string folder_name ) : its_name(folder_name) {}

    std::string name() const { return its_name ; }

    private: std::string its_name ;
};
@Moschops:

There is a problem that i am actually trying to solve that lead me down this path which also made me curious as to how the compilation and linking process is done, based on what you explained.

I have found a solution based on what was suggested .

I am just writing a helper program for myself that organizes the files on my computer.
It will read files in a specified directory and based on that name decide on where the files should be moved to. Where the files should be moved to are the folders that are specified by the user of the program, these are the destination folders. These destination folders have string parameters(specified by user) that will be compared to the name of file to determine where the file should be moved.
That solution seems to be the simplest. However like cire stated you could also use a map
(ie http://www.cplusplus.com/reference/map/map/) to pair your folders with an associated key (the key would be the name) or you could try hashing where the hashing key is a string representing the name of the directory
Topic archived. No new replies allowed.