Click here to Skip to main content
13,792,859 members
Click here to Skip to main content
Add your own
alternative version


19 bookmarked
Posted 20 Mar 2002

Simple static array class

, 20 Mar 2002
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 21:51
memberMartin Holzherr20-Mar-02 21:51 

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

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

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.181207.3 | Last Updated 21 Mar 2002
Article Copyright 2002 by John P. Curtis
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid