Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Brainstorm
  4. How to initialize a const static member?
Forum Updated to NodeBB v4.3 + New Features

How to initialize a const static member?

Scheduled Pinned Locked Moved Solved Brainstorm
6 Posts 3 Posters 3.8k Views 2 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.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by
    #1

    Hi all - sorry that this is a little long-winded.

    The scenario:

    I've written an app that communicates via a (WiFi) network with various devices via a multicast socket. The app sends messages that include identifiers for the devices (usually a serial number). Every message causes the targeted device to take some action, and send an acknowledgement.

    The problem:

    When I was designing this, it wasn't envisioned that there might be multiple users of the app on the same network. Currently, when a device sends an ack message, it's read by all of the instances of the app. This causes a little confusion.

    The solution:

    The app needs a unique identifier, which it will add to each message. The targeted device will echo the identifier, and the app will filter on this. I was planning on using QUuid for this.

    The glitch:

    How do I create and initialize this QUuid exactly once? It seems that I need a const static member variable in my Message class, but I don't know how to initialize it. I tried declaring it like this:

    class Message
    {
    private:
        const static QString m_uuid;
    ...
    

    But where/how do I initialize it? I can't do it in the Message c'tor, because I'll get a new uuid each time. I tried following an example I saw on another forum:

    const QString m_uuid = QUuid::createUuid().toString();
    
    Message::Message()
    {
        qDebug() << m_uuid;
    }
    

    But the compiler gives me an undefined reference error (on the QDebug() line).

    So...how do I do this?

    If it seems that I'm going about this the wrong way entirely, please tell me.

    Thanks...

    JonBJ 1 Reply Last reply
    0
    • mzimmersM mzimmers

      Hi all - sorry that this is a little long-winded.

      The scenario:

      I've written an app that communicates via a (WiFi) network with various devices via a multicast socket. The app sends messages that include identifiers for the devices (usually a serial number). Every message causes the targeted device to take some action, and send an acknowledgement.

      The problem:

      When I was designing this, it wasn't envisioned that there might be multiple users of the app on the same network. Currently, when a device sends an ack message, it's read by all of the instances of the app. This causes a little confusion.

      The solution:

      The app needs a unique identifier, which it will add to each message. The targeted device will echo the identifier, and the app will filter on this. I was planning on using QUuid for this.

      The glitch:

      How do I create and initialize this QUuid exactly once? It seems that I need a const static member variable in my Message class, but I don't know how to initialize it. I tried declaring it like this:

      class Message
      {
      private:
          const static QString m_uuid;
      ...
      

      But where/how do I initialize it? I can't do it in the Message c'tor, because I'll get a new uuid each time. I tried following an example I saw on another forum:

      const QString m_uuid = QUuid::createUuid().toString();
      
      Message::Message()
      {
          qDebug() << m_uuid;
      }
      

      But the compiler gives me an undefined reference error (on the QDebug() line).

      So...how do I do this?

      If it seems that I'm going about this the wrong way entirely, please tell me.

      Thanks...

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @mzimmers
      If it were useful it would be:

      /*static*/ const QString Message::m_uuid = QUuid::createUuid().toString();
      

      [My usual untested ;-)] However, before you go any further, I am not sure your initialization expression would be good for a const or static. You are not supposed to call any Qt stuff till after you have created the QApplication, so I'm not sure this is safe here? QUuid may require that, say if it uses any platform calls.

      mzimmersM 1 Reply Last reply
      3
      • JonBJ JonB

        @mzimmers
        If it were useful it would be:

        /*static*/ const QString Message::m_uuid = QUuid::createUuid().toString();
        

        [My usual untested ;-)] However, before you go any further, I am not sure your initialization expression would be good for a const or static. You are not supposed to call any Qt stuff till after you have created the QApplication, so I'm not sure this is safe here? QUuid may require that, say if it uses any platform calls.

        mzimmersM Offline
        mzimmersM Offline
        mzimmers
        wrote on last edited by
        #3

        @JonB oh, of course...I was missing the class identifier...duh.

        Your proviso notwithstanding, it appears to work: When I start my app, it generates several messages, and they all have identical UUIDs. Thanks!

        1 Reply Last reply
        2
        • Kent-DorfmanK Offline
          Kent-DorfmanK Offline
          Kent-Dorfman
          wrote on last edited by
          #4

          Hopefully this will add to your understanding of what is possible.

          https://stackoverflow.com/questions/64175121/can-i-initialize-static-const-class-member

          mzimmersM 1 Reply Last reply
          4
          • Kent-DorfmanK Kent-Dorfman

            Hopefully this will add to your understanding of what is possible.

            https://stackoverflow.com/questions/64175121/can-i-initialize-static-const-class-member

            mzimmersM Offline
            mzimmersM Offline
            mzimmers
            wrote on last edited by
            #5

            @Kent-Dorfman thanks for that link. Turns out to be a surprisingly complicated subject, doesn't it? Particularly with the changes to C++ in 11 and 17 (per the article).

            JonB's solution is simple, and seems to work, so I'm probably going to stick with it.

            1 Reply Last reply
            0
            • Kent-DorfmanK Offline
              Kent-DorfmanK Offline
              Kent-Dorfman
              wrote on last edited by Kent-Dorfman
              #6
              This post is deleted!
              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