What is the proper / acceptable design for accessing application variables from a window class?

My immediate context is the Windows platform, however I could also ask the same question when working with ui for another gui host. I work in fairly simple C ++ winapi, without ATL / MFC. I am not interested in using global variables, but rather a more practical practice of performing tasks related to "Window" with data from the "Application".

I looked at the implementation of mvvw or mvc style templates, but before I go on, I would like to see some of the community views that I imagine are countless experienced developers and designers.

There are Window members in my application class. Should the Window class be developed with reference to the application? Or is there a better way than this?

+7
source share
1 answer

The Win Window API gives you Set / GetProperty (), which allows you to define a pointer to everything you need (i.e. your own window object). However, from experience I know that it is rather slow.

Another possibility is to use a map in which the Win API window pointer is used as keys and the value is your window object. It is much faster, but where do you put this map if you do not have any global variables?

As suggested by Samuel, singleton allows you to get an object that is almost the same as global. Then you can get your window object using the Win API window pointer as a key, and it will return your object.

This is necessary to map incoming events to your window objects. Everything else, in any case, should be done the other way around (as you would expect, call functions only on your window object, which appears just like system windows.)

Why aren't you using Qt? it is already in C ++, and you do not need to worry about these details ...

+1
source

All Articles