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

Started by
6 comments, last by Dezachu 10 years, 8 months ago

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.

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?

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

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

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

I would suggest (and this is a general rule) that if you find it hard to leverage the language to fit your design, you need to rethink your design.

In this case, I suggest that your design should require some kind of integer-value button class that can be contained in your window class, and the button class can be explicitly associated with a class member. Yes, you then need to go and explicitly associate the buttons with the objects, and yes when your code changes you will need to change your code. Turns out subtlety and inexplicit indirection are actually a very bad idea in maintainable code.

Keep in mind C++ is not an interpreted language. It does not have reflection.

Stephen M. Webb
Professional Free Software Developer

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

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];

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

BSc Computer Games Programming (De Montfort University) Graduate

Worked on Angry Birds Go! at Exient Ltd

Co-founder of Stormburst Studios

This topic is closed to new replies.

Advertisement