Re: Lookup Table
From: Mike Wahler (mkwahler_at_mkwahler.net)
Date: 05/04/04
- Next message: Barry Hynes: "Trouble implementing Singleton Pattern"
- Previous message: Queazer: "Lookup Table"
- In reply to: Queazer: "Lookup Table"
- Next in thread: Francis Glassborow: "Re: Lookup Table"
- Reply: Francis Glassborow: "Re: Lookup Table"
- Reply: Arthur J. O'Dwyer: "Re: Lookup Table"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Barry Hynes: "Trouble implementing Singleton Pattern"
- Previous message: Queazer: "Lookup Table"
- In reply to: Queazer: "Lookup Table"
- Next in thread: Francis Glassborow: "Re: Lookup Table"
- Reply: Francis Glassborow: "Re: Lookup Table"
- Reply: Arthur J. O'Dwyer: "Re: Lookup Table"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|