Click here to Skip to main content
11,932,734 members (55,788 online)
Click here to Skip to main content
Add your own
alternative version


19 bookmarked

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.

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