QString as parameter in switch case



  • Hi All

    Is there any way to use QString as a parameter in switch case as shown below.
    @
    QString str;
    switch(str)
    {
    case "A":
    break;
    case "B":
    break;
    }
    @



  • You can use an string as the condition of switch statement, as long as the string class provide an operator such as:

    bq.
    operator int() const

    But, you cannot code something like this!!

    bq.
    case "A":

    From ISO C++ 6.4:

    bq.
    The value of a condition that is an initialized declaration in a switch statement is the value of the declared variable if it has integral or enumeration type, or of that variable implicitly converted to integral or enumeration type otherwise.

    From ISO C++ 6.4.2

    bq.
    Any statement within the switch statement can be labeled with one or more case labels as follows:
    case constant-expression :
    where the constant-expression shall be an integral constant.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.