when to use static class object pointer ?
-
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 ?
-
@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, astatic int array[...]
, nothing to do with objects or pointers (or arrays). -
@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.!
-
@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, astatic int array[...]
, nothing to do with objects or pointers (or arrays). -
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.
-
-
@SGaist said in when to use static class object pointer ?:
model view paradigm.
Can you provide the reference for "model view paradigm." ?
-