Jump to content
  • Advertisement
Sign in to follow this  
Conny14156

How to count how many "Insert Variable type" in a class object?

This topic is 1791 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

Hi,

I been trying to find a way to count how many variable there is inside a class, as a example would be the nummers of int inside classA. Am not sure if this is possible but seeing I could't find a proper way to google it I thougth I would ask it.

 

Share this post


Link to post
Share on other sites
Advertisement

I usually use my eyes for that sort of thing, maybe my fingers and toes as a rudimentary counting device too.

 

Are you interested in that anyway? Are you looking for the sizeof operator instead? All(*) container classes from the standard library have a size() method which returns number of objects in the container and a capacity() method which has the amount of objects actually allocated.

 

(*) to my knowledge anyway, maybe a few esoteric containers don't know how many elements they contain?

Share this post


Link to post
Share on other sites

I usually use my eyes to Xd, but this time its not me who needs to count it but functionA. am trying to make a window to draw X amount of button/textfields depending on the contain inside classA, I could just hard code it into the functionA seeing I know whats is inside classA. But that would require me to remodify it everytime I change the nummers of variable inside classA.

 

Another problem would be that I need to make a functionB for classB which would probably looks identical to functionA except it would draw 1 less button. I don't really mind doing that, but seeing that I have Y classes, I thougth maybe I would spend alot of time in doing a more dynamic way for it now. so I don't need to have 1000 functionAaa to funtionZzz. I remembered that unity had something similar with they scripting where the variable would appear on the editor, so I thougth that they had to know how many varible there was so I tired to google about it, but googling unity really didnt help :l 

Edited by Conny14156

Share this post


Link to post
Share on other sites

You mean that when you have a class like

class Blabla 
{
    public :
      int x;
      int y;
      float z;
}

And you would like to get the number of let's say integer member variables in the class, from the code (not by looking at the class definition yourself)?

That's not possible, sorry.

 

You would have to put the variables to some container, for example vector<int>, then you can get the number of them. But I don't know whether this is possible in your case.

 

EDIT: I just found your second post and I'm not really clever from it, but it sounds like a vector container could be the right way for you.

class Blabla 
{
    public :
      vector<int> integerVariables;
}

Blabla a;
...
...
cout << a.integerVariables.size(); // writes the number of variables in the container
Edited by Tom KQT

Share this post


Link to post
Share on other sites

Oh well, seems I just have to redesign my idea, but atleast iknow next time this is not possible for me :l. Good thing I asked atleast Xd thanks guys

Share this post


Link to post
Share on other sites

Having to count the variables inside a class is the same as having a variable that increments itself

 

instead of having this:
 

int w;
int x;
int y;
int z;

every time you add an int, jut increase the var;

int w;
int counter_int = 1;

or instead of having that many ints, use an array

int i_data[x];

Share this post


Link to post
Share on other sites

There's no simple way of doing it I guess... You could use a map though?

std::map<string, int> myVars;

That way you can assign a name to each of the int vars you're storing as well as have the number of them on demand:

myVars.size();

All you need now is a function which would return that map from ClassA. 

 

That's how I'd do it anyway bud smile.png

Edited by Dezachu

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!