473,395 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Initializing static arrays with enum indices

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
Nov 4 '09 #1
3 7260
weaknessforcats
9,208 Expert Mod 8TB
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.
Nov 4 '09 #2
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.

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.
Nov 4 '09 #3
weaknessforcats
9,208 Expert Mod 8TB
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.
Nov 4 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Ajax Chelsea | last post by:
can not the "static const int" be replaced by "static enum" anywhere? is it necessary that define special initialization syntax for "static const int"?
8
by: Josh Lessard | last post by:
Given a union definition: union problem_t { int mask; struct { int indices; int ops; } comp; };
34
by: Christopher Benson-Manica | last post by:
If an array is sparse, say something like var foo=; foo=4; foo='baz'; foo='moo'; is there a way to iterate through the entire array? --
8
by: Klaus Schneider | last post by:
Hi all! I'm having trouble with a template function with variable arguments when I parse an enum type as variable argument. Example: template <class T> bool test(int num, ...) { va_list ap;...
10
by: Bart Goeman | last post by:
Hi, I have a question about how to put redundant information in data structures, initialized at compile time. This is often necessary for performance reasons and can't be done at run time (data...
10
by: Russell Shaw | last post by:
Hi, gcc-3.4 complains about non-integers in: enum {IDENTIFIER = "<identifier>", WIDGETDEF = "widgetdef"}; even if i cast the strings to integers.
6
by: Marc Scheuner [MVP ADSI] | last post by:
Folks, I was trying to achieve the following thing: I have a "structure" that basically is a two-dimensional array of numeric values. Both dimensions are handled by enumeration types in C#: ...
12
by: jimmij | last post by:
Hi, Please look at the code bellow /*******************/ class ctab { private: static const unsigned n=48;
4
by: jayharris | last post by:
I'm having a ton of trouble initializing a multi-dimensional array inside a constructor, largely because I don't know the size of the array until runtime. I have a class that looks like this: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.