Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Creating an enum like structure for data types other than integral?
Forum Updated to NodeBB v4.3 + New Features

Creating an enum like structure for data types other than integral?

Scheduled Pinned Locked Moved Solved C++ Gurus
2 Posts 2 Posters 668 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.
  • fcarneyF Offline
    fcarneyF Offline
    fcarney
    wrote on last edited by
    #1

    When creating enums we can get do something like this:

    enum Vals{
        Val1,
        Val2,
        Val3
    };
    

    Then access them Vals::Val2.
    I wanted something similar, but for strings. The closest I could get was this:

    struct Token{
        QString Invalid = " ";
        QString P1 = "X";
        QString P2 = "O";
    } tokens;
    

    Then accessing them like this: tokens.P1

    Are there other, better ways to make enum like structures for data types other than integrals?
    I could also use a map, hash, etc of course. I just like the Enum::Value syntax.

    C++ is a perfectly valid school of magic.

    jsulmJ 1 Reply Last reply
    1
    • fcarneyF fcarney

      When creating enums we can get do something like this:

      enum Vals{
          Val1,
          Val2,
          Val3
      };
      

      Then access them Vals::Val2.
      I wanted something similar, but for strings. The closest I could get was this:

      struct Token{
          QString Invalid = " ";
          QString P1 = "X";
          QString P2 = "O";
      } tokens;
      

      Then accessing them like this: tokens.P1

      Are there other, better ways to make enum like structures for data types other than integrals?
      I could also use a map, hash, etc of course. I just like the Enum::Value syntax.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @fcarney said in Creating an enum like structure for data types other than integral?:

      Are there other, better ways to make enum like structures for data types other than integrals?

      Not really, but you can "improve" your struct based solution by making the members static: then you don't have to create an instance of the struct and you can do:

      Token::Invalid
      

      Also, since enum values cannot be changed you should make the struct elements const:

      struct Token{
          static const QString Invalid;
          static const QString P1;
          static const QString P2;
      }
      
      const QString Token::Invalid = " ";
      const QString Token::P1 = "X";
      const QString Token::P2 = "O";
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3

      • Login

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