Click here to Skip to main content
11,812,712 members (72,161 online)
Click here to Skip to main content

Simple static array class

, 20 Mar 2002 58.9K 19
Rate this:
Please Sign up or sign in to vote.
A simple static array class that provides range checking and non-zero indexing


CStaticArray is a template based class that encapsulates a C style single dimensional static array.

// CStaticArray
// provides:
// 1. Range checking
// 2. Non-zero indexing
// 3. Simple initialisation
template <class T,long cFirstIndex,long cLastIndex>
class CStaticArray
    CStaticArray(const T& t){Initialise(t);}

    long  GetSize()const    {return cLastIndex-cFirstIndex+1;}
    long  GetFirstIndex()const {return cFirstIndex;}
    long  GetLastIndex()const  {return cLastIndex;}
    bool  IsValidIndex(long nIndex) const 
        return nIndex>=GetFirstIndex() && nIndex<=GetLastIndex();
    size_t GetNoOfBytes()const  
        return GetSize()*sizeof(T);

    T& operator[](long nIndex)
        return m_Data[nIndex-cFirstIndex];

    const T& operator[](long nIndex)const
        return m_Data[nIndex-cFirstIndex];

    void  Initialise(const T& t)
        for(long i=0;i<GetSize();i++)

    void Initialise(long nStart,long nEnd,const T& t)
        for(long i=nStart;i<=nEnd;i++)

    operator const T*()const {return m_Data;}
    operator T*()            {return m_Data;}

    T  m_Data[cLastIndex-cFirstIndex+1];


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

John P. Curtis
Web Developer
Australia Australia
John Curtis runs Fatlab Software Pty. Ltd. which provides software services to companies in Perth, Australia and is currently developing a range of productivity apps.
He has 10+ years experience writing C++ software and 3 years writing Java code.
Specialities include STL, Boost, XML, OpenGL, Realtime control, Embedded Linux, Windows CE....

You may also be interested in...

Comments and Discussions

GeneralSTL compiant static array at www. Pin
Martin Holzherr20-Mar-02 20:51
memberMartin Holzherr20-Mar-02 20:51 
The presented static array has the disadvantage, that
it cannot be initialized like a built-in C-Array,

An alternative is an
STL compliant static array,implented as aggregate
(no user-declared constructor,no private or protected
non static members),
which allows the following kind of initialization:

boost::array<int,4> a = { { 1, 2, 3 } };

An implementation can be found at

A boost-array one can use the STL algorithms
and it can also easily be passed as a C array.
   Regards Martin Holzherr

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.151002.1 | Last Updated 21 Mar 2002
Article Copyright 2002 by John P. Curtis
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid