Dynamically allocating class objects - a question

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, 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? :

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
Last edited on
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.
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
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class AllocateInConstructor {
   // ... make it so this class calls new/delete in constr/destructor
};
class AllocateManually {
   // ... make it so this class allows programmer to call Init and Destroy
};

void Test(AllocateInConstructor aic) {};
// Copy Constructor Called: extra new/delete will be called


void Test(AllocateManually am) {};
// Copy Constructor Called: direct copy of pointer, no new/delete required implicitly.
Last edited on
Yes, that's a very good point to note as well, thanks for the example.
Topic archived. No new replies allowed.