Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Home » Tip Bank » C++
Language: C++
Expertise: All
Nov 28, 2001

Use enum Types in switch Statements


Certain compiler and code analyzers can detect a missing enumerator in a switch statement. Consider:
 
enum Seasons{
 Winter,
 Spring,
 Summer,
 Fall
};
switch (s)
{
case Winter:
  wearCoat();
  break;
case Spring:
  pruneGarden();
  break;
case Summer:
  goSwimming();
  break;
  // forgot to include a case for 'Fall'
}

The programmer listed only three cases in the switch statement, omitting Fall by accident. A clever compiler can catch this omission and warn the programmer that the switch statement misses one of the enumerators. Had we used plain constants rather than an enum type, this omission would have passed unnoticed.
Danny Kalev
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date