473,545 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Initializing static arrays with enum indices

2 New Member
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::getLexTy peName(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 7283
weaknessforcats
9,208 Recognized Expert Moderator Expert
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
theBNich
2 New Member
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 Recognized Expert Moderator Expert
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
12343
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
6702
by: Josh Lessard | last post by:
Given a union definition: union problem_t { int mask; struct { int indices; int ops; } comp; };
34
4145
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
3443
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; int ind;
10
4752
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 structures are read only) Ideally one should be able to put the redundant information there automatically so no mistakes are possible, but in a...
10
10486
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
6147
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#: public enum Level = { low, medium, high }; public enum Foo = { bar, subbar, superbar }; Now my idea was to create a two-dimensional array using...
12
2964
by: jimmij | last post by:
Hi, Please look at the code bellow /*******************/ class ctab { private: static const unsigned n=48;
4
4389
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: class MyClass { public: const int size; MyClass( const int ); };
0
7484
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7415
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7440
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7775
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5997
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4963
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3470
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3451
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1902
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.