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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
class InputClass
{
public:
InputClass();
~InputClass();
void Initialize();
void KeyDown(unsigned int input);
void KeyUp(unsigned int input);
bool IsKeyDown(unsigned int);
private:
bool m_keys[256];
};
|
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
class SystemClass
{
SystemClass();
~SystemClass();
bool Initialize();
void Shutdown();
void Run();
LRESULT CALLBACK MessageHandler(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
//...
InputClass* m_Input; // <-------
};
|
And in Initialize(), initialized like this:
1 2 3 4 5 6
|
bool SystemClass::Initialize()
{
m_Input = new InputClass;
//...
}
|
And, as expected,
delete
d in Shutdown().
My question is: What advantage does dynamically allocating this object accomplish, rather than simply doing the following in the definition of SystemClass? :
1 2 3 4 5 6 7
|
//SystemClass.h
class SystemClass
{
//...
InputClass m_Input;
//...
|
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