Sign in to follow this  
Conny14156

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

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

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

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this