How to rewrite complicated lines of C++ code (nested ternary operator)

后端 未结 7 1574
误落风尘
误落风尘 2020-12-05 23:29

I\'ve been looking through someone else\'s code for debugging purposes and found this:

!m_seedsfilter ? good=true : m_seedsfilter==1 ? good=newClusters(Sp) :         


        
7条回答
  •  南方客
    南方客 (楼主)
    2020-12-05 23:51

    if ( !m_seedsfilter )
      good = true;
    else if ( m_seedsfilter == 1 )
      good = newClusters(Sp);
    else
      good = newSeed(Sp);
    

    An expression followed by ? corresponds roughly to if ( expression ) while the : introduces something similar to an else clause. Note that this is an expression rather than a statement, i.e.

     ?  : 
    

    is an expression whose value is expression-1 if condition is true, otherwise it is expression-2.

提交回复
热议问题