I have a few comments. You start off correctly by attaching pointers to the actual variables. But you then try to delete the pointers. You can't. They point to real objects, not dynamic memory. And if you allocate memory to the pointers, then they cannot point to the variables since they now point to dynamic memory. The reason you use new is because you need a chunk of memory. Not to "allocate a pointer".
1)
int real = 256;
int * ptr_real = ℜ
2)
int * ptr_dyn = new int;
delete ptr_dyn;
The point is, you won't delete ptr_real, since it doesn't point to dynamic memory.
Ok. The next suggestion I have may or may not appeal to you, just an idea:
Code:
class WindowClass : public WNDCLASS {
public:
UINT Style( int new_style = -1 ){
if(new_style != -1) style = (UINT)new_style;
return style;
}
UINT AddStyle( UINT additional ){
return Style( additional | Style() );
}
UINT RemoveStyle( UINT remove ){
return Style( Style() & ~remove );
}
WNDPROC ProcedureVariable( WNDPROC new_proc = 0 ){
if(new_proc) lpfnWndProc = new_proc;
return lpfnWndProc;
}
int ClassExtraBytes(int amount = -1){
if(amount != -1) cbClsExtra = amount;
return cbClsExtra;
}
int ExtraBytes(int amount = -1){
if(amount != -1) cbWndExtra = amount;
return cbWndExtra;
}
HINSTANCE Instance(HINSTANCE some_other_instance = 0){
if(some_other_instance) hInstance = some_other_instance;
return (HINSTANCE)hInstance;
}
HICON Icon(HICON new_icon = 0){
if(new_icon) hIcon = new_icon;
return hIcon;
}
HCURSOR Cursor(HCURSOR new_cursor= 0){
if(new_cursor) hCursor = new_cursor;
return hCursor;
}
HBRUSH Background(HBRUSH new_background = 0){
if(new_background) hbrBackground = new_background;
return hbrBackground;
}
LPCSTR MenuName(LPCSTR new_name = 0){
if(new_name ) const_cast<char*>(lpszMenuName) = new_name;
return lpszMenuName;
}
LPCSTR ClassName(LPCSTR new_name = 0){
if(new_name ) const_cast<char*>(lpszClassName) = new_name;
return lpszClassName;
}
void Reset(){
memset(this, 0, sizeof(*this));
}
WindowClass(){
Reset();
Style(CS_HREDRAW|CS_VREDRAW);
Instance(GetModuleHandle(NULL));
Background((HBRUSH)GetStockObject(WHITE_BRUSH));
Icon(LoadIcon(NULL,IDI_APPLICATION));
Cursor(LoadCursor(NULL,IDC_ARROW));
}
};
The major advantages of this setup are:
1) Can be passed into functions expecting a WNDCLASS struct.
3) Exploit the constructor mechanism to create defaults.
4) Add as many member functions as you can think of.
5) Use same functions to get/set data.
Prerequisite:
- Only add member functions, don't add more variables to the structure.
Let's see it in action now:
Code:
typedef HWND WindowHandle;
typedef MSG WindowMessage;
typedef HINSTANCE WindowInstance;
typedef HDC DeviceHandle;
typedef HICON WindowIcon;
typedef HCURSOR WindowCursor;
typedef HBRUSH WindowBrush;
typedef WNDPROC WindowsProcedureVariable;
static TCHAR szAppName[]=TEXT("Hungarians Suck");
WindowHandle window;
WindowMessage message;
WindowClass _class;
LRESULT CALLBACK WindowsProcedure(WindowHandle,UINT,WPARAM,LPARAM);
int WINAPI WinMain (WindowInstance instance,WindowInstance prevInstance,LPSTR CommandLine,int ShowCommand)
{
_class.AddStyle(CS_DBLCLKS);
_class.ProcedureVariable(WindowsProcedure);
_class.Instance(instance);
_class.ClassName( TEXT("Hungarians Suck") );
if (!RegisterClass(&_class))
{
MessageBox(NULL,TEXT("This Program Requires Windows NT"),szAppName,MB_ICONERROR);
return 0;
}
window=CreateWindow(_class.ClassName(),
TEXT("Hello World From The Anti-Hungarian Society"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
_class.Instance(),
NULL);
ShowWindow(window,ShowCommand);
UpdateWindow(window);
while (GetMessage(&message,NULL,0,0))
{
TranslateMessage(&message);
DispatchMessage(&message);
}
return message.wParam;
}
LRESULT CALLBACK WindowsProcedure (WindowHandle window, UINT message, WPARAM wParam, LPARAM lParam)
{
DeviceHandle DeviceHandle;
PAINTSTRUCT pstruct;
RECT rect;
switch (message)
{
case WM_CREATE:
return 0;
case WM_PAINT:
DeviceHandle=BeginPaint(window,&pstruct);
GetClientRect(window,&rect);
DrawText(DeviceHandle,TEXT("Hungarians Suck!"),-1,&rect,
DT_SINGLELINE|DT_CENTER|DT_VCENTER);
EndPaint(window,&pstruct);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(window,message,wParam,lParam);
}