我在堆上创建 System 对象,在系统类内部我在堆上创建 Game 对象,而在堆栈上创建 KeyboardServer 对象。
1)KeyboardServer 对象是否会像在堆上一样,因为 System 对象是它的一部分?
2) 是否也需要在堆上创建 KeyboardServer 对象?
3)有没有更好的解决方案来提高性能?
////////////////////////////////////////////////////////////////////////////////
// Filename: main.cpp
////////////////////////////////////////////////////////////////////////////////
#include "SystemClass.h"
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR pScmdline, int iCmdshow )
{
SystemClass* System;
bool result;
// Create the system object
System = new SystemClass;
if ( !System )
{
return 0;
}
// Initialize and run the system object
result = System->Initialize();
if (result)
{
System->Run();
}
// Shutdown and release the system object
System->Shutdown();
delete System;
System = 0;
return 0;
}
////////////////////////////////////////////////////////////////////////////////
// Filename: SystemClass.h
////////////////////////////////////////////////////////////////////////////////
#ifndef _SYSTEMCLASS_H_
#define _SYSTEMCLASS_H_
///////////////////////////////
// PRE-PROCESSING DIRECTIVES //
///////////////////////////////
#define WIN32_LEAN_AND_MEAN
//////////////
// INCLUDES //
//////////////
#include <Windows.h>
///////////////////////
// MY CLASS INCLUDES //
///////////////////////
#include "GameClass.h"
#include "KeyboardClass.h"
////////////////////////////////////////////////////////////////////////////////
// Class name: SystemClass
////////////////////////////////////////////////////////////////////////////////
class SystemClass
{
public:
SystemClass();
~SystemClass();
bool Initialize();
void Shutdown();
void Run();
LRESULT CALLBACK MessageHandler( HWND hwnd, UINT umsg, WPARAM wparam, LPARAM lparam );
private:
void InitializeWindows();
void ShutdownWindows();
private:
LPCSTR m_applicationName;
HINSTANCE m_hinstance;
HWND m_hwnd;
GameClass* m_Game;
KeyboardServerClass m_KeyboardServer;
};
/////////////////////////
// FUNCTION PROTOTYPES //
/////////////////////////
static LRESULT CALLBACK WndProc( HWND hwnd, UINT umessage, WPARAM wparam, LPARAM lparam );
/////////////
// GLOBALS //
/////////////
static SystemClass* ApplicationHandle = 0;
#endif
////////////////////////////////////////////////////////////////////////////////
// Filename: KeyboardClass.h
////////////////////////////////////////////////////////////////////////////////
#ifndef _KEYBOARDCLASS_H_
#define _KEYBOARDCLASS_H_
////////////////////////////////////////////////////////////////////////////////
// Class prototype
////////////////////////////////////////////////////////////////////////////////
class KeyboardServerClass;
////////////////////////////////////////////////////////////////////////////////
// Class name: KeyboardClientClass
////////////////////////////////////////////////////////////////////////////////
class KeyboardClientClass
{
public:
KeyboardClientClass( const KeyboardServerClass& KeyboardServer );
~KeyboardClientClass();
bool KeyIsPressed( unsigned char keycode ) const;
private:
const KeyboardServerClass& server;
};
class KeyboardServerClass
{
friend KeyboardClientClass;
public:
KeyboardServerClass();
void OnKeyPressed( unsigned char keycode );
void OnKeyReleased( unsigned char keycode );
private:
static const int nKeys = 256;
bool keystates[ nKeys ];
};
#endif