Hi, I currently have an enumeration in class Lexer: - enum lexType {
-
-
/* token keywords */
-
lexIF, lexTHEN, lexWHILE, lexDO, lexBEGIN, lexEND, lexELSE, lexPROGRAM,
-
-
...
-
-
/* used for array iterations */
-
lexENUMSIZE
-
};
I have an array used to get the string representations of these types: - const char* lexTypeNames[lexENUMSIZE];
This array is currently populated in the class's constructor: - Lexer::Lexer() {
-
lexTypeNames[lexIF] = "lexif";
-
lexTypeNames[lexTHEN] = "lexthen";
-
lexTypeNames[lexWHILE] = "lexwhile";
-
lexTypeNames[lexDO] = "lexdo";
-
lexTypeNames[lexBEGIN] = "lexbegin";
-
lexTypeNames[lexEND] = "lexend";
-
lexTypeNames[lexELSE] = "lexelse";
-
...
-
}
Finally, I have a method in the class to retrieve the string representation given a lexType: - const char* Lexer::getLexTypeName(lexType type) const {
-
return lexTypeNames[type];
-
}
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: - 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
3 7283
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.
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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"?
|
by: Josh Lessard |
last post by:
Given a union definition:
union problem_t {
int mask;
struct {
int indices;
int ops;
} comp;
};
|
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?
--
|
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;
|
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...
| |
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.
|
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...
|
by: jimmij |
last post by:
Hi,
Please look at the code bellow
/*******************/
class ctab
{
private:
static const unsigned n=48;
|
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 );
};
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
| |