473,386 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

create a variable name with the value of another variable

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!
Jul 22 '05 #1
3 1775
Brian wrote:
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[s] = YourClass();

Jul 22 '05 #2
Brian wrote:
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.

Jul 22 '05 #3
"Brian" <nl*****@aol.com> wrote in message
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.
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Geoff Winsor | last post by:
Hi, I am experiencing a problem with recalling a session variable which stores whether a person is logged in to a "members only" section of a website. This area of the site has been working...
7
by: Rolf Kemper | last post by:
Dear All, somehow I remember that such or similar question was discussed already somewhere. But I can't find it anymore. I have a template calling itself. As long it goes deeper into the...
10
by: sam | last post by:
Hi, I m wondering why I can't declare reference variable for default value in a function argument variable? eg. class A { void f(string &str="");
8
by: Steve Neill | last post by:
Can anyone suggest how to create an arbitrary object at runtime WITHOUT using the deprecated eval() function. The eval() method works ok (see below), but is not ideal. function Client() { }...
7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
23
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
4
by: etuncer | last post by:
Hello All, I have Access 2003, and am trying to build a database for my small company. I want to be able to create a word document based on the data entered through a form. the real question is...
5
by: Lee | last post by:
I have a xml file, here is sample part: <?xml version="1.0" encoding="UTF-8"?> <ProducsList> <Product id="1"> <SpecList> <Spec> <SpecLabel>Height</SpecLabel> <SpecValue>10</SpecValue>...
4
by: Michael Munch | last post by:
Hi I want to read the value of af text-field, create dynamic, in a form. Se below a small test-site to do that (but readning fails): I use the function Test_Read for reading the value from the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.