| Ogoyant (153) | |||||||||
|
Hello all, I'm reading some code from some samples at rastertek.com. I find a definition of a class meant to store the current keyboard state, similar to this:
In the definition of the "system class" of the application, meant to store variables such as window handles, contain functions such as the application's Windows mesage handling process, etc. , the Input class is used and allocated dynamically like this:
And in Initialize(), initialized like this:
And, as expected, deleted in Shutdown(). My question is: What advantage does dynamically allocating this object accomplish, rather than simply doing the following in the definition of SystemClass? :
InputClass objects don't need to be more than one for the duration of the application's running time, nor does InputClass have any dynamically allocated variables internally. It consists of an array of 256 bools that are necessary for the program to function. So why allocate dynamically in the first place? I am asking in case there are advantages of dynamic allocation that I am not aware of. Thanks a lot in advance for any replies. Ogoyant | |||||||||
|
Last edited on
|
|||||||||
| EssGeEich (1007) | |
|
1. The object will not be destroyed when a function ends (Goes out of scope) 2. You can dinamically allocate ultra-sized arrays with runtime-chosen size. | |
|
|
|
| Ogoyant (153) | |
|
Thank you EssGeEich, that's what I knew as the advantages of dynamic allocation. Even though in this particular case dynamically allocating arrays is not required, and the system class object will not go out of scope until the time the application closes. Thank you very much for the feedback, much appreciated! Ogoyant | |
|
Last edited on
|
|
| EssGeEich (1007) | |||
|
Well if you want me to say something more, in this case you can pass this object by value with less troubles. Make this example:
| |||
|
Last edited on
|
|||
| Ogoyant (153) | |
| Yes, that's a very good point to note as well, thanks for the example. | |
|
|
|