Finding elements of a class
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
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement