FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


[c++]Using all the members of a class





masterevil
i have created a function that must use all the members of a class. I'll type an easy one for you:
Code:

class inventory
{
//codes
}

and a function that uses that class:
Code:

int pInv(inventory a, inventory b, inventory c.....)
{
//codes
}

I have coded 15 items for the inventory so far. But to show the inventory screen, i have to type all the items again:

Code:

pInv(short_sword, long_sword ..... /*all the items i have coded*/);

That would be waste of memory and more than that, typing such a long code would be hard. Is there a code that calls all the members of a class?
for example, i'd love to use that sort of code:
Code:

pInv(ALLTHEMEMBERS);

Much more easy to type and change.

is there such code?
kv
The ultimate solution would have been introspection, but C++ doesn't support it.

A quick solution will be to define an alias using #define.

Put this in top of your file after your #includes.

Code:

#define ALLTHEMEMBERS short_sword, long_sword .....


and use ALLTHEMEMBERS insted of the long list.
It will be replaced by list during compile time by compiler.
mogyoro
Use arrays or maps, its much more simple and effective...
Shin
Yes Mogyaro is right, you should use array. e.g, inventory[NUMITEMS].
Related topics
Frihost n Google Adsense
What to learn first?
CD/DVD suspciously stops working
Editing a Css class with javascript?
Programming in a lot of languages
Airconditioning in cars
PHP mailing script
[VB6]Making a Scriptable Macro/Engine
Functional Programming
Is Good marks everything????
which Programing launguage to learn
JSP Help!!!
10 frih$ contest: what does this c++ code do?
C++ Using class methods in inline functions? Scope problem?
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.