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. Are global objects a good idea?
Forum Updated to NodeBB v4.3 + New Features

Are global objects a good idea?

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 5 Posters 896 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.
  • L Offline
    L Offline
    Leon
    wrote on last edited by
    #1

    Hi

    I am getting back into development after a long time, so my question might be "dump"

    I have an object called ImageFetcher. My options are:

    • Allow mixed use of imageFetcher-> and ImageFetcher:: (so some classes would receive the object, some would not)
    • Send the object to every class that needs it, so allow only the use of imageFetcher->
    • Create a global object, accesible by every class, to use with imageFetcher->

    I personally think that the global approach is the most "clean". I guess there would be some "safety" issues, so this should be done carefully.

    Christian EhrlicherC A 2 Replies Last reply
    0
    • L Leon

      Hi

      I am getting back into development after a long time, so my question might be "dump"

      I have an object called ImageFetcher. My options are:

      • Allow mixed use of imageFetcher-> and ImageFetcher:: (so some classes would receive the object, some would not)
      • Send the object to every class that needs it, so allow only the use of imageFetcher->
      • Create a global object, accesible by every class, to use with imageFetcher->

      I personally think that the global approach is the most "clean". I guess there would be some "safety" issues, so this should be done carefully.

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Leon said in Are global objects a good idea?:

      I personally think that the global approach is the most "clean".

      No, global objects a.k.a. singletons are an anti-pattern.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      L 1 Reply Last reply
      0
      • Christian EhrlicherC Christian Ehrlicher

        @Leon said in Are global objects a good idea?:

        I personally think that the global approach is the most "clean".

        No, global objects a.k.a. singletons are an anti-pattern.

        L Offline
        L Offline
        Leon
        wrote on last edited by
        #3

        @Christian-Ehrlicher

        Thank you! That's exactly what i wanted to hear.

        Between the first two then what should i prefer? Is the first also an anti-pattern?

        SGaistS 1 Reply Last reply
        0
        • L Leon

          @Christian-Ehrlicher

          Thank you! That's exactly what i wanted to hear.

          Between the first two then what should i prefer? Is the first also an anti-pattern?

          SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          Before answering you need to provide more details.

          How many classes need to access it ?
          How are they suppose to use it ?
          You seem to have static methods, what would they be used for ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          L 1 Reply Last reply
          1
          • SGaistS SGaist

            Hi,

            Before answering you need to provide more details.

            How many classes need to access it ?
            How are they suppose to use it ?
            You seem to have static methods, what would they be used for ?

            L Offline
            L Offline
            Leon
            wrote on last edited by
            #5

            @SGaist

            I definitely need to reconsider which methods need to be static.

            To my understanding, even if the method is 100 lines, and only one line has a Q_EMIT, then that method shouldn't be static. Is that correct?

            JonBJ 1 Reply Last reply
            0
            • L Leon

              @SGaist

              I definitely need to reconsider which methods need to be static.

              To my understanding, even if the method is 100 lines, and only one line has a Q_EMIT, then that method shouldn't be static. Is that correct?

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

              @Leon
              Only an object/instance can emit a signal. Assuming you mean emit someSignal() that cannot be in a static method. You could have some object emit someObject->someSignal() in a static method, but that is probably not what you have in mind.

              1 Reply Last reply
              1
              • L Leon

                Hi

                I am getting back into development after a long time, so my question might be "dump"

                I have an object called ImageFetcher. My options are:

                • Allow mixed use of imageFetcher-> and ImageFetcher:: (so some classes would receive the object, some would not)
                • Send the object to every class that needs it, so allow only the use of imageFetcher->
                • Create a global object, accesible by every class, to use with imageFetcher->

                I personally think that the global approach is the most "clean". I guess there would be some "safety" issues, so this should be done carefully.

                A Offline
                A Offline
                Asperamanca
                wrote on last edited by
                #7

                @Leon What is imageFetcher supposed to do?

                L 1 Reply Last reply
                0
                • A Asperamanca

                  @Leon What is imageFetcher supposed to do?

                  L Offline
                  L Offline
                  Leon
                  wrote on last edited by
                  #8

                  Thank you all for your answers! They have been really helpful

                  @Asperamanca

                  I didn't want to dive into the specifics of the class, as i am searching for general design patterns

                  A 1 Reply Last reply
                  0
                  • L Leon has marked this topic as solved on
                  • L Leon

                    Thank you all for your answers! They have been really helpful

                    @Asperamanca

                    I didn't want to dive into the specifics of the class, as i am searching for general design patterns

                    A Offline
                    A Offline
                    Asperamanca
                    wrote on last edited by
                    #9

                    @Leon Then my answer is: They are usually not a good idea, but there are valid exceptions.

                    1 Reply Last reply
                    1

                    • Login

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