I'm just starting C++ but I've done quite a bit in Java and VB. I've learnt a lot over the last few days from all the tutorials and books and MSDN of course, but the thing I'm finding hardest to learn is not how to program but how to make programs .
What I mean is when I'm writing code I never know if what I'm doing is the 'right' thing to do or if there are better ways to do what I want. I try to think about the best object-oriented way to accomplish certain tasks.
Anyway the problem I have at the moment is that I want to create an object which will exist for the duration of the program. I want to construct the object when the program starts (IE when the WM_INITDIALOG message is sent). But I want to be able to access the object at other times during the program's excecution such as when the user clicks on the window. At the moment I have this code:
This works fine but as soon as the DialogProc ends the object goes out of scope and is destroyed.Code:HDC hdc = GetDC(hWnd); MapTiler mp(hdc,100,4,4); ReleaseDC(hWnd,hdc);
Is there a way I can decalare the object outside of the DialogProc scope so that I can access it at any time? Note that I have to call the object's constructor while passing the HDC of the window as a parameter. In Java this would be easy. I could declare the object as an attribute of the program's class and then instantiate it when the program started. It seems though that C++ requires you to instantiate the object at the same time as declaring it which means I can't declare the object outside the scope DialogProc and instantiate it inside the DialogProc scope.
What is the best way to get around this? Should I use a pointer to the object? Should I use a global variable at all? In my opinion using global variables is not a very object-oriented technique so maybe there's an entirely different way of doing this.