Jump to content
  • Advertisement
Sign in to follow this  

Question about win32 window templates

This topic is 1480 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have been working through Microsoft's "Learn to Program for Windows in C++" articles found here


Learn to Program for Windows in C++




and have a question about the end of module 1: http://msdn.microsoft.com/en-us/library/windows/desktop/ff381400%28v=vs.85%29.aspx


In their sample they derive a window from a base class, where the base class is as follows:

template <class DERIVED_TYPE>
class BaseWindow
    static LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
        DERIVED_TYPE *pThis = NULL;

I am just trying to understand why this is created as a template. My understanding of class templates is to allow us to write a class, and use that class for any data type we want. Eg MyVector<int> or MyVector<double>


In this case I cant understand why they have used a template class. Can they just have gone

class MainWindow : public BaseWindow

Instead they have used

class MainWindow : public BaseWindow<MainWindow>

I just cant understand what other kinds of windows can be "derived" from this base case? I can understand in the case of calling a function with an int or a float, and needing a template there, but not in this case.


Thanks for your time

Share this post

Link to post
Share on other sites
This is what is commonly known as the Curiously Recurring Template Pattern.

Basically, the base class needs to know the derived class' type for some reason.

In this particular case it is used to dispatch the windows messages to your class. They store your this pointer into the special window storage location on window creation, and then retrieve it when a message comes into the static window message handler so they can call the virtual message handler on it.

Granted, I am not entirely sure why they did it this way. BaseWindow has the virtual message handler function, so they could have just stored the BaseWindow this pointer and dispatched that way.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!