Re: Lookup Table

From: Mike Wahler (mkwahler_at_mkwahler.net)
Date: 05/04/04


Date: Tue, 04 May 2004 19:27:09 GMT


"Queazer" <queazer@nospam.beerdrinkers.co.uk> wrote in message
news:ySRlc.516$fi6.25@newsfe1-win...
> I'm trying to code a lookup table in the form of a constant array for use
> by the member functions of a class. Ideally, this should be visible in the
> header file and should be initialised at compile-time rather than in the
> constructor of the object or similar.
>
> What I would like to do is something like:
>
> public:
> typedef enum {red, green, yellow, blue, magenta, cyan} tColour;
>
> protected:
> const int r[tColour] = {255, 0, 255, 0, 255, 0};
> const int g[tColour] = { 0, 255, 255, 0, 0, 255};
> const int b[tColour] = { 0, 0, 0, 255, 255, 255};
>
> ...but it doesn't compile. With or without 'static'.
>
> Am I on completely the wrong track?

Yes, you're trying invalid syntax.

1. Above 'tColour' is a type, not a value. You try to use
   this type as a value (to specify the sizes of your arrays).
   An array's size must be specified with a constant unsigned
   integer expression.

2. If your arrrays (or any other type data members) are nonstatic,
   initializers are not allowed. Initialization must be done with
   a constructor.

3. But arrays cannot be used in a ctor init-list, so their elements
   must be assigned in the ctor body.

4. For static data members, only their declarations should appear in
   the class body. The definitions (and initializers) must appear
   at file scope.

class X
{
public:
    typedef enum
        {red, green, yellow, blue, magenta, cyan, colour_count} tColour;

protected:
    const static int r[colour_count];
    const static int g[colour_count];
    const static int b[colour_count];
};

const int X::r[colour_count] = {255, 0, 255, 0, 255, 0};
const int X::g[colour_count] = { 0, 255, 255, 0, 0, 255};
const int X::b[colour_count] = { 0, 0, 0, 255, 255, 255};

-Mike



Relevant Pages

  • Multi-dimension array assignments
    ... If the initializer of a subaggregate or contained union ... matching right brace initialize the elements or members of the subaggregate ... only enough initializers from the list ... I end up with all 3 arrays having the same values: ...
    (comp.lang.c)
  • Re: Multi-dimension array assignments
    ... If the initializer of a subaggregate or contained union begins with a left brace, the initializers enclosed by that brace and its matching right brace initialize the elements or members of the subaggregate or the contained union. ... guess is "yes" as array members need to be contiguous in memory, so arrays of arrays members would also be contiguous in memory as they are arrays too. ...
    (comp.lang.c)
  • Re: Multi-dimension array assignments
    ... If the initializer of a subaggregate or contained union ... > matching right brace initialize the elements or members of the subaggregate ... only enough initializers from the list ... > guess is "yes" as array members need to be contiguous in memory, ...
    (comp.lang.c)
  • Wide string initializer syntax
    ... Looking through the C90 standard, it occurred to me that the possible ... syntaxes for initializers, particularly of wchar_t arrays, are really ... larger-dimensional arrays right up to the environment limits. ...
    (comp.lang.c)
  • Re: Getting rid of the C RTL
    ... according to a standard or am I just not looking at it ... If there are fewer initializers in the list than there are members ... Arrays of characters are special cases, ...
    (microsoft.public.vc.language)