Switch statement: must default be the last case?

后端 未结 10 2172
面向向阳花
面向向阳花 2020-11-27 02:36

Consider the following switch statement:

switch( value )
{
  case 1:
    return 1;
  default:
    value++;
    // fall-through
  case 2:
    ret         


        
10条回答
  •  感动是毒
    2020-11-27 02:57

    There are cases when you are converting ENUM to a string or converting string to enum in case where you are writing/reading to/from a file.

    You sometimes need to make one of the values default to cover errors made by manually editing files.

    switch(textureMode)
    {
    case ModeTiled:
    default:
        // write to a file "tiled"
        break;
    
    case ModeStretched:
        // write to a file "stretched"
        break;
    }
    

提交回复
热议问题