Finding elements of a class

Started by
2 comments, last by Gresc 20 years, 8 months ago
I kow you can find how many elements in an array by doing this: int array[10] int num = sizeof(array) / sizeof(int); but how do you do that in a class? Say if i have like so: MyClass[20][10]; i dont know why i want to know this, just curious i guess
Advertisement
How about sizeof(array)/sizeof(class)

But it will only give you the size of the class and it''s data members, not it''s functions (I think), and if your class contains pointers, strings etc, then you still won''t get an accurate number.

If you really need to do it, you might be better to add a size function to your class, and just sum all the sizes of all the array elements.
quote:Original post by Bagpuss
But it will only give you the size of the class and it''s data members, not it''s functions (I think), and if your class contains pointers, strings etc, then you still won''t get an accurate number.


Non-virtual functions aren''t part of the object (though if virtual functions are involved, there''s a v-table). And pointers/strings are just other variables; sure, they may point to memory elsewhere, but that memory isn''t part of the object.

If you just want to calculate the number of elements in the array, sizeof(array)/sizeof(class) works fine.

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

yeh thanx i tested it and it did work

This topic is closed to new replies.

Advertisement