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


Should I use an array in a class?

Recommended Posts

Arrays are one of the most powerful things in C++ and there is nothing wrong in using them in classes. Without arrays we would have to name everything Item1, Item2, ect. which would make trying to control anything with more than 6 or so elements a real mess. Also, putting an array inside your class shouldn''t make messier code.


int Array[20];

for(int x=0; x<20; x++)
Array[x] = x;

Looks alot better than this:

int Num1, Num2, Num3, Num4, Num5, Num6, Num7, Num8, Num9, Num10, Num11, Num12, Num13, Num14, Num15, Num16, Num17, Num18, Num19, Num20;

Num1 = 0;
Num2 = 1;
Num3 = 2;
Num4 = 3;
Num5 = 4;
Num6 = 5;
Num7 = 6;
Num8 = 7;
Num9 = 8;
Num10 = 9;
Num11 = 10;
Num12 = 11;
Num13 = 12;
Num14 = 13;
Num15 = 14;
Num16 = 15;
Num17 = 16;
Num18 = 17;
Num19 = 18;
Num20 = 19;

Hope this help


Share this post

Link to post
Share on other sites
Let''s look at three scenarios.

Scenario #1: You don''t know how many elements of the array will actually be used, but even a fully used array only requires say, 1k or less, and you know that you will only need a few instances of this class. In that case, I say, use the array.

Scenario #2: You know exactly how many elements of the array will be used: full capacity. And you also know that all the size of the array is not huge, and you have decided that all the classes will be using the array data consistently and often. In that case, use the array.

Scenario #3: The array may be huge, it might not be fully utilized, or you need many instances of the classes, and each class may only need the array during certain times. In that case, I would look at dynamic memory allocation and linked lists.

Share this post

Link to post
Share on other sites
Acctually, you''d probably do better to use an array than a linked list, unless you might need a huge amount of numbers. IMO, Linked lists are better to use with large classes that take alot of memory, no with small types such as int''s. The main reason for this is link lists can get pretty ugly and will take up about 3 times the amount of memory as an array of int''s would (at least the way i do them). Either way is good, just depends on what you need and what you feel comfortable with i guess.

Share this post

Link to post
Share on other sites
If I make it into an array, I know it would be more complicated code,
but wouldn''t it save more space and speed?

No, in fact, a class is something like an array. Classes can be called typedef struct, some compilers allow this, and a struct access is being translated to asm just like

for(i=0; i < xy; ++i)
// ...
mov somewhere, arrays'' base address + i*sizeof(single element)

There is no real difference. Sometimes, its more nice to use arrays, for instance:

  class CClass
void AnyFunction()
memset(m, 0, sizeof(int)*2);
int m[2];

However, you can replace the memset-call above with:

  memset((int*)this, 0, sizeof(this));  

That is dangerous, if you change the the order of the elements, things could screw up, because the compiler has no idea of what you are doing, and perhaps you are deleting things you don''t want to delete.

I think in this case, the important thing is what kind of integers these are. Is it a player''s flags, his IP address, his ID, etc., or is it just some 20 integers? I mean, if they have something logically associated with it, such as size, health, etc., use elements. If the class has to store 20 integers it doesn''t know anything about, use an array to keep things clear.


Share this post

Link to post
Share on other sites
try this. my arraylist. kinda like java
but c++

#define SAFE_DELETE(lp) { if(lp) { delete lp;lp = NULL; }}

template <class D>
class CArrayList
CArrayList(): m_pData(NULL) , m_nCapacity(0), m_nSize(0) { }
virtual ~CArrayList() { SAFE_DELETE(m_pData); }

void capacity(int n)
if(m_nSize == 0)
m_pData = new D[n];
memset(m_pData,0,sizeof(D) * n);
m_nCapacity = n;
} else
if(n > m_nCapacity)
D * t_pData;
t_pData = new D[n];
memset(t_pData,0,sizeof(D) * n);
m_nCapacity = n;
memcpy(t_pData,m_pData,m_nSize * sizeof(D));
m_pData = t_pData;

int capacity() { return m_nCapacity; }

int size() { return m_nSize; }

void push_back(D * lp)
if(m_nCapacity == 0)

memcpy(m_pData + m_nSize,lp,sizeof(D));

if(m_nSize == m_nCapacity / 2)
capacity(m_nCapacity * 2);

void pop_back()

typedef D * iterator;

iterator begin() { return m_pData; }
iterator end() { return m_pData + m_nSize; }

void erase(iterator it)
swap(it,m_pData + m_nSize);

void swap(iterator it1,iterator it2)
D t;


D * m_pData;
int m_nCapacity;
int m_nSize;

its like java arraylist.

{ Stating the obvious never helped any situation !! }

Edited by - jwalker on October 15, 2001 7:25:14 PM

Share this post

Link to post
Share on other sites
vector''s are very good .
the grow exponentially though.

1 tip is to reserve your vector before using it.
Common mistake is inserting elements into a vector
without reserving it yourself..

myvec.reserve(25); // reserve 25 blocks

this speeds up your vector ..

{ Stating the obvious never helped any situation !! }

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
If you know how many elements ahead of time you want in the array, then use the ARRAY!!!!!!!!!!!!!!!!!!

The only time vectors are really needed are when you dont know how many elements/want to create more dynamically.


Share this post

Link to post
Share on other sites
not exactly .

if you are reading records from a file, you can calculate
how many records are in the file.

but then the number of records in memory can grow.
so use a vector if you can .

{ Stating the obvious never helped any situation !! }

Share this post

Link to post
Share on other sites