A simple Enum method (idiom) in C/C ++)

Source: Internet
Author: User

I used this idiom again when I wrote the program today, so I posted it by the way. This idiom is quite simple and is probably used by many people. Today, I am posting it for reference to new users (it takes time for veterans to read this post ).
To illustrate how to use this technique, let's give a simple example. For example, to develop a network program, you need to count the number of packets of various network protocols.

Version 1

Assume that you only need to process the HTTP and FTP protocols at the beginning. Some people will immediately declare the following two integers for statistics without thinking about them:

Int ncnthttp = 0;
Int ncntftp = 0;


It seems that there is no problem. However, if the requirement changes, two additional protocols are required: SMTP and SSH. Then, the students continue to expand the above Code to the following:

Int ncnthttp = 0;
Int ncntftp = 0;
Int ncntsmtp = 0;
Int ncntssh = 0;


At this time, the problem began to emerge. For example, if you want to print the preceding four statistical values, you have to write four printf columns. If you want to use assertions to ensure that all statistical values are greater than zero, you have to write four assert columns. This is all annoying. (Of course, some students write the printing of four variables in one printf, but it is still annoying)

Version 2

How can this be done? Some of the above Code is changed to the array form, and the above four statistical valuesSequential
In the array. The details are as follows:

Int ncntproto [4];
/* 0th are HTTP, 1st are FTP, 2nd are SMTP, and 3rd are SSH */


In this way, both printing and assertions can be done through the for loop, which seems quite convenient. However, this introduces another problem. Suppose I want to use SMTP statistics in the program, so I have to write the code: ncntproto [2

]. This creates an unsightly "magic number"
"! You know, magic number is one of the stinks of code.
). In the future, if the storage order in the array changes, it will be finished: a lot of code that uses magic number has to be changed. If you miss something, there are countless bugs!

Version 3

In order to eliminate the magic number and increase the code readability and maintainability, some people began to work with the idea of enum. An Enum group is added to the Code as follows:

enum PROTO
{
PROTO_HTTP,
PROTO_FTP,
PROTO_SMTP,
PROTO_SSH,
};

int nCntProto[4];

In this way, if I need to use SMTP statistics, I do not need to write ncntproto [2], but ncntproto [proto_smtp]. In this way, the readability is much better. Even if the storage order of the array changes in the future, it does not matter: you only need to slightly adjust the order of constants in Enum, other code does not need to be moved.

Version 4

However, there is still a bad thing. The magic number "4" is used to define the array statement. In the event that the demand changes in the future and the agreement is added, the number must be adjusted constantly. Unhappy!
At this time, the ultimate version was unveiled. See the following code:

Enum proto
{
Proto_http,
Proto_ftp,
Proto_smtp,
Proto_ssh,

Proto_num/* indicates the number of protocols */
};

Int ncntproto [proto_num];

The advantage of this writing method is that,None
Magic number. Whether it is to reference a statistical value or to traverse the array cyclically, the defined constant is used.
When the requirement changes, you need to add a new protocol. You only need to add the corresponding Enum constant to the Enum (but remember to ensure that proto_num is at the end of the enum definition ). Because proto_num will automatically grow, other code will be almost unaffected.

C ++ supplementary description

The above Code applies to both C and C ++. However, for some C ++ programmers, they may not get used to the original array and think that the STL container class looks quite pleasing to the eye. There is no big deal: as long as the array declaration of the above Code is modified to the following, the other code is basically the same.

STD: vector <int> vctcntproto (proto_num );


Copyright Notice

All original articles in this blog are copyrighted by the author. This statement must be reprinted to keep this article complete, and the author's programming will be noted in the form of hyperlinks
And the original address of this article:

Http://program-think.blogspot.com/2009/04/c-cxx-enum-idiom.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.