Alex Rivera | Logout

Do all C++ compilers allow using a static const int class member variable as an array bound?

Asked 2009-08-25T11:54:34.530
9

In VC++ when I need to specify an array bound for a class member variable I do it this way:

 class Class {

 private:
     static const int numberOfColors = 16;
     COLORREF colors[numberOfColors];
 };

(please don't tell me about using std::vector here)

This way I have a constant that can be used as an array bound and later in the class code to specify loop-statement constraints and at the same time it is not visible anywhere else.

The question is whether this usage of static const int member variables only allowed by VC++ or is it typically allowed by other widespread compilers?

Edit
Report

1 Answer

3

Besides other answers you can use following function do determine number of elements in statically alocated arrays:

template<typename T, size_t length>
size_t arrayLength(T (&a)[length])
{
    return length;
}
answered 2009-08-25T12:53:49.923

Your Answer