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. when to use static class object pointer ?
Forum Update on Monday, May 27th 2025

when to use static class object pointer ?

Scheduled Pinned Locked Moved Solved C++ Gurus
7 Posts 5 Posters 1.4k Views
  • 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.
  • Q Offline
    Q Offline
    Qt embedded developer
    wrote on 4 Aug 2023, 09:35 last edited by Qt embedded developer 8 Apr 2023, 09:35
    #1

    In my code i have found several places the

    below type of code is written :

    static task * taskmanager[NUM_TASKS];
    

    i want to know when to use static class object pointer ?

    if possible give me real time example that explain concept why we should use ?

    P J 2 Replies Last reply 4 Aug 2023, 09:58
    0
    • Q Qt embedded developer
      4 Aug 2023, 09:35

      In my code i have found several places the

      below type of code is written :

      static task * taskmanager[NUM_TASKS];
      

      i want to know when to use static class object pointer ?

      if possible give me real time example that explain concept why we should use ?

      J Offline
      J Offline
      JonB
      wrote on 4 Aug 2023, 10:03 last edited by JonB 8 Apr 2023, 10:04
      #3

      @Qt-embedded-developer
      Note that in addition to @PR_stark's explanation there is nothing special about the static being a "class object pointer" as opposed to anything else. His comments would apply just as much to, say, a static int array[...], nothing to do with objects or pointers (or arrays).

      S 1 Reply Last reply 4 Aug 2023, 10:41
      1
      • Q Qt embedded developer
        4 Aug 2023, 09:35

        In my code i have found several places the

        below type of code is written :

        static task * taskmanager[NUM_TASKS];
        

        i want to know when to use static class object pointer ?

        if possible give me real time example that explain concept why we should use ?

        P Offline
        P Offline
        PR_stark
        wrote on 4 Aug 2023, 09:58 last edited by
        #2

        @Qt-embedded-developer, I am not sure the context you're working on.

        Example of when to use

        So, One real-life example I can think of is in a game development where you want to keep track of all the players' scores.

        Instead of creating a separate score list for each player, you use a static class object pointer to have a common score list that all players can access and update.

        This way, every player can see the latest scores, and you don't need to duplicate the score list for each player.

        By using a static class object pointer, I can ensure that all instances of the class share the same array of pointers, and thus, all players' scores are accessible from anywhere within the program.

        And regarding the question explain concept why we should use?

        In general, to use static class object pointers when you have a clear need for shared data that requires a global scope.

        If the shared data is specific to a particular instance of the class or a local scope, it's better to use regular instance variables or pass data explicitly through function parameters.

        Hope this answers your question.!

        1 Reply Last reply
        1
        • Q Qt embedded developer
          4 Aug 2023, 09:35

          In my code i have found several places the

          below type of code is written :

          static task * taskmanager[NUM_TASKS];
          

          i want to know when to use static class object pointer ?

          if possible give me real time example that explain concept why we should use ?

          J Offline
          J Offline
          JonB
          wrote on 4 Aug 2023, 10:03 last edited by JonB 8 Apr 2023, 10:04
          #3

          @Qt-embedded-developer
          Note that in addition to @PR_stark's explanation there is nothing special about the static being a "class object pointer" as opposed to anything else. His comments would apply just as much to, say, a static int array[...], nothing to do with objects or pointers (or arrays).

          S 1 Reply Last reply 4 Aug 2023, 10:41
          1
          • J JonB
            4 Aug 2023, 10:03

            @Qt-embedded-developer
            Note that in addition to @PR_stark's explanation there is nothing special about the static being a "class object pointer" as opposed to anything else. His comments would apply just as much to, say, a static int array[...], nothing to do with objects or pointers (or arrays).

            S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 4 Aug 2023, 10:41 last edited by
            #4

            Hi,

            Please take good note: global shared data is usually the first step into big troubles. 99% of the time you will end with a monstrosity that makes your code hard to follow because it relies on states that will not be clear. Also you will have changes to that global data that will be sprinkled through your code with very hard to follow interaction such as why is this value X although you changed it to Y a couple of seconds ago. Hence, avoid them like the plague unless you really really really know what you are doing. There are other means to share data across several objects such as using the model view paradigm.

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

            Q 2 Replies Last reply 4 Aug 2023, 11:13
            1
            • S SGaist
              4 Aug 2023, 10:41

              Hi,

              Please take good note: global shared data is usually the first step into big troubles. 99% of the time you will end with a monstrosity that makes your code hard to follow because it relies on states that will not be clear. Also you will have changes to that global data that will be sprinkled through your code with very hard to follow interaction such as why is this value X although you changed it to Y a couple of seconds ago. Hence, avoid them like the plague unless you really really really know what you are doing. There are other means to share data across several objects such as using the model view paradigm.

              Q Offline
              Q Offline
              Qt embedded developer
              wrote on 4 Aug 2023, 11:13 last edited by
              #5

              @SGaist Dear i am reading the code of existing software. i have to use it in for further development if it comes.

              thanks @JonB @PR_stark @SGaist

              1 Reply Last reply
              0
              • Q Qt embedded developer has marked this topic as solved on 4 Aug 2023, 11:13
              • S SGaist
                4 Aug 2023, 10:41

                Hi,

                Please take good note: global shared data is usually the first step into big troubles. 99% of the time you will end with a monstrosity that makes your code hard to follow because it relies on states that will not be clear. Also you will have changes to that global data that will be sprinkled through your code with very hard to follow interaction such as why is this value X although you changed it to Y a couple of seconds ago. Hence, avoid them like the plague unless you really really really know what you are doing. There are other means to share data across several objects such as using the model view paradigm.

                Q Offline
                Q Offline
                Qt embedded developer
                wrote on 24 Feb 2024, 08:21 last edited by
                #6

                @SGaist said in when to use static class object pointer ?:

                model view paradigm.

                Can you provide the reference for "model view paradigm." ?

                C 1 Reply Last reply 24 Feb 2024, 08:24
                0
                • Q Qt embedded developer
                  24 Feb 2024, 08:21

                  @SGaist said in when to use static class object pointer ?:

                  model view paradigm.

                  Can you provide the reference for "model view paradigm." ?

                  C Online
                  C Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 24 Feb 2024, 08:24 last edited by
                  #7

                  @Qt-embedded-developer lmgtfy: https://doc.qt.io/qt-6/model-view-programming.html

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

                  1 Reply Last reply
                  1

                  7/7

                  24 Feb 2024, 08:24

                  • Login

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