Archived

This topic is now archived and is closed to further replies.

Gresc

Finding elements of a class

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites