Switch statement: must default be the last case?

后端 未结 10 2167
面向向阳花
面向向阳花 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 03:06

    Chiming in with another example: This can be useful if "default" is an unexpected case, and you want to log the error but also do something sensible. Example from some of my own code:

      switch (style)
      {
      default:
        MSPUB_DEBUG_MSG(("Couldn't match dash style, using solid line.\n"));
      case SOLID:
        return Dash(0, RECT_DOT);
      case DASH_SYS:
      {
        Dash ret(shapeLineWidth, dotStyle);
        ret.m_dots.push_back(Dot(1, 3 * shapeLineWidth));
        return ret;
      }
      // more cases follow
      }
    

提交回复
热议问题