Archived

This topic is now archived and is closed to further replies.

Sorting through class parts

This topic is 5133 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Im working on a 'game' and I need to cycle either through all the number in a part of a class, but I cant figure out how. e.g. Class number { public: int set; int group; }number_one, number_two, number_three; // there are a lot more of these now, if someone typed in 'number_one' into a string, how would i be able to find the number in number_one.set from that information e.g. cout << what number? cin >> number_one cout << "number_one.set = 7"; how could i scroll through that. Do i have to use if's? I just read a different post, would tokenizing work here? if so, how would i do it? BoC Homepage Luck is a Horse to ride like any other...Luckily im not a gambler, I dont know how to ride. [edited by - betrayer_of_code on November 26, 2003 8:46:16 PM] [edited by - betrayer_of_code on November 26, 2003 8:47:11 PM]

Share this post


Link to post
Share on other sites
I think you''ll find that it is. You want to use an object name at run time, but objects only have names at compile time. This is what is discussed in that other thread.

Share this post


Link to post
Share on other sites
quote:
Original post by Betrayer_of_Code
Im working on a 'game' and I need to cycle either through all the number in a part of a class, but I cant figure out how.
e.g.
Class number
{
public:

int set;
int group;
}number_one, number_two, number_three; // there are a lot more of these


I'm not sure I understand you fully, but I'll have a bash anyway. I think what you're saying is that you'd like to loop through all of the numbers from the created objects? As in, if you have 5 objects, you could loop through each one, printing the number in turn, for example?

If that is the case, then you can use an array of class Number. Let's imagine that you have 5 of these Number objects. In the style in which you've created these objects, they would have the following names:

number_one
number_two
number_three
number_four
number_five

Instead of this, you can use an array that has 5 (0 to 4) elements in it. Like so:

Number number_array[5]; // Array of 5 Number's.

This way, you can loop through them in the following fashion:


for (int i = 0; i < 5; i++)
cout << "number_array[" << i << "].set = " << number_array[ i ].set << endl;


Barring any typos (long day, apologies in advance), hopefully this will be what you're looking for.

-hellz

Edit: Stupid italics and my poor use of terminology.

[edited by - hellz on November 27, 2003 6:23:00 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by downgraded
Or have a Macro

#define MY_MACRO(x) number_x


No. That won''t work.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites