Connecting Tech Pros Worldwide Forums | Help | Site Map

Initializing static arrays with enum indices

Newbie
 
Join Date: Nov 2009
Posts: 2
#1: 3 Weeks Ago
Hi, I currently have an enumeration in class Lexer:

Expand|Select|Wrap|Line Numbers
  1. enum lexType {
  2.  
  3.     /* token keywords */
  4.     lexIF, lexTHEN, lexWHILE, lexDO, lexBEGIN, lexEND, lexELSE, lexPROGRAM,
  5.  
  6.     ...
  7.  
  8.     /* used for array iterations */
  9.     lexENUMSIZE
  10. };
I have an array used to get the string representations of these types:

Expand|Select|Wrap|Line Numbers
  1. const char* lexTypeNames[lexENUMSIZE];
This array is currently populated in the class's constructor:

Expand|Select|Wrap|Line Numbers
  1. Lexer::Lexer() {
  2.     lexTypeNames[lexIF]             =   "lexif";
  3.     lexTypeNames[lexTHEN]           =   "lexthen";
  4.     lexTypeNames[lexWHILE]          =   "lexwhile";
  5.     lexTypeNames[lexDO]             =   "lexdo";
  6.     lexTypeNames[lexBEGIN]          =   "lexbegin";
  7.     lexTypeNames[lexEND]            =   "lexend";
  8.     lexTypeNames[lexELSE]           =   "lexelse";
  9.     ...
  10. }
Finally, I have a method in the class to retrieve the string representation given a lexType:

Expand|Select|Wrap|Line Numbers
  1. const char* Lexer::getLexTypeName(lexType type) const {
  2.     return lexTypeNames[type];
  3. }
The problem with this set up is that if another class has a lexType and I simply want to get the string representation of this lexType, I have to create a new instance of the Lexer class before I can call getLexTypeName(). Therefore, I was hoping there was some way to make getLexTypeName() static so I could simply call Lexer::getLexTypeName(lexType). I know I could do something like this:

Expand|Select|Wrap|Line Numbers
  1. static const char* lexTypeNames[] = {"lexif", "lexthen", "lexwhile", "lexdo", ...};
However, this seems like a very error-prone solution that depends on the order of the enumerated elements for a correct initialization. Is there some way to statically initialize the array by manually specifying the indices like I did in the constructor? If not, is there a more elegant solution for this?

Thank you,
Brian

Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,384
#2: 3 Weeks Ago

re: Initializing static arrays with enum indices


You should not have to a) use static arrays or b) access array elements directly and c) use arrays at all.

I suggest you convert your array to a vector. vectors are required to be implemented as arrays so you still have your array. But you also have the vector code to manage the array instead of writing it all over again.

Put the vector inside a class and make all access to that vector go through a class member function. That will hide the vector.

Next, make this a Singleton class. Singleton classes are classes that can have only one instance. If you do that then there is one instance of your array that all classes can use.

Read the Insight article on the Singleton class. Complete information is there on how to set this up.
Newbie
 
Join Date: Nov 2009
Posts: 2
#3: 3 Weeks Ago

re: Initializing static arrays with enum indices


Thanks for the Singleton suggestion--I've implemented it, and it's a nice solution. However, I'm not sure I understand why to use vectors instead of arrays here.

Quote:
I suggest you convert your array to a vector. vectors are required to be implemented as arrays so you still have your array. But you also have the vector code to manage the array instead of writing it all over again.
In general, I do use vectors over arrays for the reasons you stated. In this case, however, I'm simply creating an array, mapping strings to each index, and then accessing each index without any further modifications to the array thereafter. How do vectors help rather than arrays in this case?

Thanks.
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,384
#4: 3 Weeks Ago

re: Initializing static arrays with enum indices


A vector is required to be implemented as an array.

The vector member functions manage that array. No more screw-ups and memory corruptions.

OR:

You can create your own arrays.

Write code that duplicates vector<> member functions. Time sink.

Write code differently each time. Maximize your maintenance costs.

Write code that's slower than vector<>. Almost always the case. STL templates are all optimized ofr speed.


My rule is: Unless you can write down on paper why a vector<> won't work, then use the vector<>.

It gets worse. STL does not want you to write any more linked lists, trees, loops, etc since there are replacing templates for all these things (and a whole lot more). The hard part is that since you spent 5000 years learning how to do all this stuff, to ditch that in favor of some whiz-bang templates is tough.

It goes easier to just give up at the outset.
Reply

Tags
array, enum, initialize, static