Archived

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

johnnyBravo

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

Recommended Posts

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 this post


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


Link to post
Share on other sites
quote:
Original post by pinacolada
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 :/

Interesting reading: http://c2.com/cgi/wiki?JavaArraysShouldBeFirstClassObjects

Share this post


Link to post
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

Share this post


Link to post
Share on other sites