Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QString as parameter in switch case

QString as parameter in switch case

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 13.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    Indrajeet
    wrote on last edited by
    #1

    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;
    }
    @

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dbzhang800
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved