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. Initialize members of global const class
Qt 6.11 is out! See what's new in the release blog

Initialize members of global const class

Scheduled Pinned Locked Moved C++ Gurus
6 Posts 4 Posters 2.9k Views 3 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.
  • T3STYT Offline
    T3STYT Offline
    T3STY
    wrote on last edited by T3STY
    #1

    I have a class which is made like this:

    class myClass{
    public:
    QString value1;
    QString value2;
    QString value3;
    QFont someFont;
    };
    

    This class is available globally and should be used as a data structure to pass data around some other classes.
    Now I would like to create a global object of that class, that should never change, which serves as fallback to restore the application in case of failure of something. I thought I'd use a global const object:

    const myClass fallbackObject;
    

    (note: the object is actually created inside my own namespace, it's not really "global")
    But how do I initialize this object's members so that they contain useful data?
    I know I could have used a constructor, but unfortunately that will cause a few side effects in my code (it would make legal a call to a method in class of mine -which I don't want to allow-, a bit hard to explain, just trust me).

    Thank you in advance

    1 Reply Last reply
    0
    • MayEnjoyM Offline
      MayEnjoyM Offline
      MayEnjoy
      wrote on last edited by
      #2

      try this:

      initFunction()
      {
      // myClass init
      }

      static bool init = initFunction();

      I am a engineer.

      1 Reply Last reply
      0
      • T3STYT Offline
        T3STYT Offline
        T3STY
        wrote on last edited by
        #3

        It's as same as the constructor method.
        Any other ideas?

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by Chris Kawa
          #4

          If you're using C++11 capable compiler you can either use uniform initialization syntax:

          const myClass fallbackObject{ "value1", "value2", "value3", QFont("Verdana") };
          

          or the non-static data member initialization:

          class myClass{
          public:
          QString value1 { "value1" };
          QString value2 { "value2" };
          QString value3 { "value3" };
          QFont someFont { QFont("Verdana") };
          };
          const myClass fallbackObject;
          

          In c++03 you can simply create a function that would return the fallback object:

          myClass getFallback() {
              myClass c;
              c.value1 = "value1";
              c.value2 = "value2";
              c.value3 = "value3";
              return c;
          }
          const myClass fallbackObject = getFallback();
          
          1 Reply Last reply
          1
          • T3STYT Offline
            T3STYT Offline
            T3STY
            wrote on last edited by
            #5

            Thank you very much, Chris! The uniform initialization syntax looks just the right tool!

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mcosta
              wrote on last edited by
              #6

              Yep,

              this is why they added in C++11 :D

              Once your problem is solved don't forget to:

              • Mark the thread as SOLVED using the Topic Tool menu
              • Vote up the answer(s) that helped you to solve the issue

              You can embed images using (http://imgur.com/) or (http://postimage.org/)

              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