My immediate context is the Windows platform, however I might also ask this same question when working on a ui for another gui host. I’m working in fairly plain c++ winapi, without ATL/MFC. I’m not interested in using globals, but rather a more oop accepted practice of performing “Window” related tasks with “Application” data.
I’ve considered implementing mvvw or mvc style patterns to this, but before I go ahead I’d like some community opinions, from what I imagine are countless experienced developers and designers.
My Application class has Window members. Should the Window class be designed with an Application reference? Or is there a better way than this?
The Win API of a Window gives you a Set/GetProperty() which allows you to define a pointer to whatever you’d like (i.e. your own window object.) However, from experience I know that’s rather slow.
The other possibility is to use a map where the pointer of the Win API windows are used as keys and the value is your window object. This is a lot faster, but where do you put that map if you don’t have any globals?
As suggested by Samuel, a singleton allows you to get an object which is pretty much the same as a global. Then you can get your window object using the Win API window pointer as the key and it returns your object.
This is required to map incoming events to your window objects. Everything else should anyway be done the other way around (As you’d expect, call functions only on your window object that are mapped in a similar way as the system windows.)
Why aren’t you using Qt? it’s already in C++ and you don’t have to worry about those details…