Title: Get information from an instance defined in runtim
Cheo - January 14, 2007 11:02 PM (GMT)
Hi
I have this class declaration in a header file called FileWithClassDeclarations:
| CODE |
#ifndef FILE_WITH_CLASS_DECLARATION_H #define FILE_WITH_CLASS_DECLARATION_H
class Hero { public: int nStrength; int nIntelligence; int nDexterity; int nEndurance; int nPersonality;
Hero(int nS, int nI, int nD, int nE, int nP);
int Health(); int Mana();
};
Hero::Hero(int nS, int nI, int nD, int nE, int nP) { nStrength = nS; nIntelligence = nI; nDexterity = nD; nEndurance = nE; nPersonality = nP; }
int Hero::Health() { return nStrength * nEndurance; }
int Hero::Mana() { return nIntelligence * nPersonality; }
#endif |
At some point during runtime, an instance of the class will be created by the user.
The data members will get their values from five variables within a .cpp file (the first variable is strength). The user select the name of the instance. If the user chose gandalf, the code for the definition would be:
| CODE |
| Hero gandalf(strength, variable2, variable3, variable4, variable5); |
I have a function that will print the information of the instance once is created. The problem is that the code of the function doesn't know the name of the instance prior to being created. What is necessary to make this code to work?
Let's say I want to print the nStrength data member of the gandalf instance. nStrength takes the value of the variable called strength. Thus, the code to print would be:
| CODE |
| cout << Hero gandalf.strength; |
Remember, the code to print the instance has to be created before the instance is defined in runtime.
Question: What is necessary to create a code that will print this instance?
Thank you in advance,
Sergio
TheHawgMaster - January 15, 2007 04:02 AM (GMT)
| QUOTE (Cheo @ Jan 14 2007, 04:02 PM) |
Let's say I want to print the nStrength data member of the gandalf instance. nStrength takes the value of the variable called strength. Thus, the code to print would be:
| CODE | | cout << Hero gandalf.strength; |
|
^ That little snippet makes absolutely no sense to me.
But are you asking how to make the object aware of it's name? Why not just add a string member for that purpose?
Shonoby - January 15, 2007 04:08 AM (GMT)
I think he wants to make it aware of its name and have the player choose an aspect which will enhance that aspect but to do so he wants to pass it to the hero thing(not looking at the code right now).
So what u should do is make a template inside the class and have it overload the hero thing, then return those results.
P->S: Can u explain something that makes sense( not an insult),
Just explain it better or have some sort of example that shows what u mean something like a flow chart or w/e just some ideas to help u convey ur idea
Cheo - January 15, 2007 11:19 AM (GMT)
Right,
I want the user to be able to define instances of the Hero class at any moment during runtime. The interface is a winAPI frame.
The frame has 5 spin controls to set the values of the the instance variables of the Hero class, an edit box to type the name of the instance and two buttons. One button creates the instance once the user is happy with the values he has set. Another button will show a dialog with the information of the instance ONCE is created.
In order to create an instance called gandalf of the class hero, we could do:
| CODE |
| Hero gandalf(strength, intelligence, dexterity, endurance, personality); |
These five variables define the data members of the class hero: nStrength, nIntelligence, nDexterity, nEndurance and nPersonality.
The user assigns the values of strength, intelligence, dexterity, endurance and personality with the spin controls, and the name of the instance with an Edit Text.
Then, when the user press a button in the frame, the instance is created. Another button will show the information of the new instance in a dialog ONCE the instance is created.
It's in this dialog where I want to implement the code to show the information. For example, if we want the code to show the value of gandalf's strength, the code would be:
| CODE |
| cout << gandalf.nStrength; |
The problem is that the program doesn't know yet the name of the instance, and thus cannot show anything (compile error: gandalf has not been declared), because the code to show the information is done before the user run the program.
What can I use instead of gandalf, so the program knows that at some point an instance name is going to be used to show the sStrength of the Hero class.
The code is waiting for the name of the instance to be input to show the information. How do I implement this?
I hope this explanation is clearer.
Shonoby - January 15, 2007 05:49 PM (GMT)
Well, i really don't know how to make the object know its name but one thing u could do is instead of having that object 'hero(...)' u should leave that as it is and make an array of w/e characters as the hero's name, then use that like so:
| CODE |
cout << heroname << endl << hero(...);
|
So that it could be less complicated more readable and i guess will substantially have more memory to use but for that just pass it all by reference.
This will be an easy want to do it because if u were gonna do it like before every time u just wanted to display his name, then it will also display his name and str etc.
which would looks bad :
| CODE |
gandalf 10 10 10 10 10 attacked monster for 50 monster hit gandalf 10 10 10 10 10 for 10
|
that would look really dumb right.
AMR, The - January 20, 2007 01:20 AM (GMT)
if i correctly understood what you said,
then you are trying to:
1. assign the name of the instance (which is given by the user) at runtime
and
2. use the name of the instance to access it
there are a few ways to do this, and the way you are trying to it is probably impossible
what youre trying to do wont work because youre trying to dynamically declare an identifier (which doesnt work in c++, in fact, it doesnt work in any compiled programming language, but you could always try packaging a compiler with the program and have it dynamically compile and link the data into the program :blink: )
how to best solve your problem depends on how many instances you need to dynamically name
if you only need to name one, you can just use a string variable to store the name, instead of trying to dynamically name the instance's identifier
but if you will have many instances of the class, and you need to be able to access them all by name, you should consider using the map class,
| CODE |
//an example of using the STL map class #include <map> #include <string> #include <iostream>
using namespace std;
int main() { map<string,int> intMap;//a map using strings as keys and integers as values string name; int value; cout << "enter a name for the integer" << endl; cin >> name; cout << "enter a value for the integer" << endl; cin >> value; intMap[name] = value;//this assigns the value of the variable value //to and key with the value of name //but since the key doesnt exist, it adds it to the map cout << "the value for the key " << name << " is: " << intMap[name] << endl; } |