#### Archived

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

# I want to make size() function like in the vector class

## Recommended Posts

johnnyBravo    100
You know with vectors how it has the .size() function to tell you the size of the array. Well ive got my own class
//the class

class Vertex {
public:
float x,y,z;
};

//then using it in an array

Vertex myVertices[10];

//i want a .size() function to tell me the array is 10

//eg.

int theSize = myVertices.size() //theSize is 10


how could i do this? Thanks,

##### Share on other sites
What you have is an array. Arrays don''t have a size() in C (they do in Java, it''s nice). If you want something with a size, you pretty much have to write your own Vector.

##### Share on other sites
johnnyBravo    100
ah i see, that sucks

i love all javas little features, but i so hate their really long method/function names. I can never remember them.

##### Share on other sites
Amnesty    174
If you''re using fixed sized arrays (ala built in arrays) and really want a size function then use boosts array class.

Also, you get the added goodness of begin/end functions so you can easily use STL algorithms on it aswell. Plus more gooderness

##### Share on other sites
Zahlman    1682
quote:
What you have is an array. Arrays don''t have a size() in C (they do in Java, it''s nice). If you want something with a size, you pretty much have to write your own Vector.

Ahem, Java''s arrays don''t have a size(), they have a length.

Notice, it''s written like it''s a field, but it''s apparently supposed to be an operator. When you apply the reflection interface to an array, Java will recognize it as a class in some ways but not in others - and there will be no ''length'' among the fields. The metamodel is all screwed up :/

##### Share on other sites
Guest Anonymous Poster
Also, if you''re sure that what you have is an actual array (not a pointer to the first element, or a pointer to a dynamically allocated array), you can get the size of the array:

#define array_size( x ) \
( sizeof( x ) / sizeof( (x)[0] ) )

-Brian