create a variable name with the value of another variable

B

Brian

Ok, so i know the title is a little confusing but let me put it this
way

lets say i have an array of strings, and with each string inside the
array, i want to make a new class

problems:

1) i dont know how many words there are so i can't just make classes
up ahead of time

2) i want to be able to just reference them by their names

i know in cshell codeing youcan do $x to access the value of the
variable so you could do something like

new class $x

so if x's value was "DOG" you'd have a new class called DOG

is there any way to do this in c++, not just a class, i want to be
able to make new arrays also so i guess i just need to be able to
access the value of the variable and be able to make something new
with THAT name

hope you guys/gals can understand...

thanks in advanced!
 
R

red floyd

Brian said:
Ok, so i know the title is a little confusing but let me put it this
way

lets say i have an array of strings, and with each string inside the
array, i want to make a new class

problems:

1) i dont know how many words there are so i can't just make classes
up ahead of time

2) i want to be able to just reference them by their names

i know in cshell codeing youcan do $x to access the value of the
variable so you could do something like

new class $x

so if x's value was "DOG" you'd have a new class called DOG

is there any way to do this in c++, not just a class, i want to be
able to make new arrays also so i guess i just need to be able to
access the value of the variable and be able to make something new
with THAT name

hope you guys/gals can understand...

thanks in advanced!

Look at std::map.

Specifically....


#include <map>
#include <string>

class YourClass {
//...
};

std::map <std::string,YourClass> DynamicNames;

You can now index DynamicNames by a string.

e.g.:

std::string s;
std::getline(cin, s);

DynamicNames = YourClass();
 
R

Rolf Magnus

Brian said:
Ok, so i know the title is a little confusing but let me put it this
way

lets say i have an array of strings, and with each string inside the
array, i want to make a new class

There is no way in C++ to dynamically create a type. Each type has to be
fully defined at compile time. If you think you really need that, I
suggest considering a scripting language (e.g. python) instead of C++.
problems:

1) i dont know how many words there are so i can't just make classes
up ahead of time

2) i want to be able to just reference them by their names

i know in cshell codeing youcan do $x to access the value of the
variable so you could do something like

new class $x

so if x's value was "DOG" you'd have a new class called DOG

is there any way to do this in c++, not just a class, i want to be
able to make new arrays also so i guess i just need to be able to
access the value of the variable and be able to make something new
with THAT name

The only thing you can do is use an std::map<std::string, somebaseclass>
into which you can put your objects with a name and easily find them
later by that name and use them.
 
S

Siemel Naran

Brian said:
lets say i have an array of strings, and with each string inside the
array, i want to make a new class

Look up the factory pattern in the internet or books. All your classes have
to derive from a common base.

Basically, something like:

std::map PossibleTypes<std::string, boost::shared_ptr<Base> > map;
map["A"] = boost::shared_ptr<Base>(new DerivedA());
....

But there are many variations like a map of string non-member function
pointers, details on the easiest and least error prone way to add elements
to the map, singleton objects, etc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top