The new C++ for the new Windows

This is the start of a new series of articles that I’m writing about C++ for Windows. I took some time off from my regular writing schedule to evaluate my approach to Windows programming and gain some actual production experience using the latest iteration of C++ in my new Micro ISV (check out Window Clippings).  What I will be presenting here is not the C++ you’ll find in most of the sample code currently found on MSDN. This is not about MFC. This is not even about ATL and WTL that I’ve recommended for years. This is a fresh new approach to Windows programming with C++. I don’t claim that my ideas are unique but I have found in my experience that the techniques that I’ve adopted help improve my productivity and produce better quality code, code that is easier to maintain, more reliable, and helps to enable excellent performance.

With the introduction of Visual C++ 2010 back in April of this year there was an opportunity for me to reconsider the way I develop applications for Windows. One of the unique aspects of C++ is that it is a multi-paradigm programming language. Other programming languages require you to write all your code as classes, or all of it using only functions. They might provide a handful of container types that you’re practically forced to use. Alternatively they might not provide any and force you to write your own. C++ however places no such constraints on its users. Bjarne Stroustrup sums it up nicely:

C++ was designed to support data abstraction, object-oriented programming, and generic programming in addition to traditional C programming techniques … It was not meant to force one particular programming style upon all users.

As the C++ language and the compilers that implement it have evolved and improved over the years different approaches have been popularized for writing Windows programs. To better appreciate these different approaches it is useful to consider the difference between libraries and frameworks.

Using frameworks generally means that you plug in a bit of code into a larger framework to make your application run. Frameworks usually provide a large runtime or considerable infrastructure that you need to embrace in order to take advantage of the framework. Examples of frameworks commonly used to build Windows applications today include the .NET Framework and the older Microsoft Foundation Classes (MFC).

On the other hand using a library generally means that you write your application in whatever way makes sense to you and then plug in a bit of library code to solve a particular problem in your application without dramatically affecting the way your application is written or packaged. Examples of libraries commonly used in Windows applications today include the Active Template Library (ATL) and the Windows Template Library (WTL). Of course there are many others.

There isn’t always a very clear distinction between frameworks and libraries. You can for example use ATL in such a way that it effectively becomes a framework that your code plugs into. Some approaches or libraries might even exclude the use of other libraries. So while its multi-paradigm roots have helped C++ tremendously it has also inadvertently splintered the community of developers into different camps. Many developers cannot imagine using anything other than MFC to build Windows applications. Others swear by ATL and WTL as a more flexible and relatively modern approach.

I’m not here to tell you that any one of these approaches is wrong. That’s the beauty of C++. You get to choose what works for you and your application. I am here to tell you that Visual C++ 2010 provides the building blocks for a different approach that I’ve started using. An approach based on libraries rather than frameworks.

This series is called “The new C++ for the new Windows” because I’m going to show you how you can embrace C++0x, the unofficial new standard for the C++ programming language, and use it to your advantage when writing applications for the new Windows. The “new Windows” is just a cute way to refer to those new and improved libraries in the Windows SDK that target the contemporary versions of Windows namely Windows 7 and Windows Vista. That means I have the luxury of avoiding all that nasty code in existing libraries like MFC and ATL that uses older APIs dating back to Windows 95 when far better alternatives exist today.

And that’s it for my introduction. The first few parts of this series will lay down some foundational principles and then we’ll dig in and look at how they can be used with various parts of the Windows SDK. I’m hoping to publish one installment a week and I’d love to hear what you think.

Next: Putting bugs into buckets

Console

38 thoughts on “The new C++ for the new Windows

  1. Germain

    I love your article and can’t wait for more…i havent programmed in C++ for a year now and I am so thrilled to go back and to something new as well :)

    Reply
  2. Danny

    “C++0x, the unofficial new standard for the C++ programming language”

    Excuse me, it IS the official standard for C++. Nothing ‘unofficial’ about it. The fact that MS has been slow to adopt it is another issue.

    Reply
  3. Andrey

    I gave up with C++ for Windows long ago. First Delphi, next .NET. Looking forward for something eyes opening.

    Reply
  4. Jason P Sage

    I like your approach and how you present “options” and then say “use the right tool for YOUR application” because I think c++ ( and freePascal both..less templating) are amazing in how different programming paradigms can be used with great effect in either of them… any good language I imagine…

    I too look forward to more articles :)

    Reply
  5. JusuR

    “I gave up with C++ for Windows long ago. First Delphi, next .NET. Looking forward for something eyes opening.”

    Try Qt :)! As an old MFC + WIN32 programmer, I instantly fell in love. Easy, powerful and well documented api that gives a programmer a real helping hand. I’ve learned to respect it as one of the best frameworks/libraries ever made, if the target is to build an application. Any application.

    Oh yes…and it is cross platform :)!

    Reply
    1. Andrey

      Well, I tried Qt. My feelings are mixed. Yes, this is a great framework. However, what an enormous effort spent to fight what looks like exactly one fundamental C++ limitation – lack of a proper pointer to a method of a class instance.

      I am afraid that Android will bury Symbian, next MeeGo, next Nokia, next Qt. That will be a pity, but I feel that even Silverlight is on par with Qt when the development speed is an issue.

      Reply
      1. Andrey

        Re: “Please explain:
        “one fundamental C++ limitation – lack of a proper pointer to a method of a class instance.”

        Couldn’t get it.”

        In C++ one can get a pointer to the method of a class. To call the method, it is necessary to have both the pointer to the class instance and the pointer to the method. In Delphi one can get a pointer that combines the pointer to the instance and the pointer to the method. The Delphi way allows for logical implementation of the “implementation through delegation” model. Whatever you do to duplicate that in C++ is doomed to be ugly.

        I feel that MOC and slots were introduced as the solution to this problem. I guess it is an excellent way around, the second best to choosing some other language.

        I anticipate some misunderstanding from C++ programmers based on the arguments like “oh, we can implement that”. Yes, you can implement. You cannot mandate the usage (MOC solves most of this), leaving aside proper usage. Basic features must be basic and exist on the language level.

    1. Ajay Vijayvargiya

      I do! Win32 is real pain, MFC/ATL is just thin wrapper around Windows API, and there is no bloating. Do wizards support Win32 messages-mapping into “switch-case” ?

      May be you don’t have much or UI.

      Reply
  6. Eric

    “One of the unique aspects of C++ is that it is a multi-paradigm programming language.”
    If this was a unique aspect, it would be THE multi-paradigm programming language, which it isn’t.

    Reply
    1. Kenny Kerr Post author

      That might have come across a bit too strong. I was referring to commercially successful programming languages targeting Windows (C, C++, C#, VB, Java) and did not mean to imply that C++ is unique in the universe of programming languages which of course it is not.

      Reply
  7. rebob

    I was a career C / C++ programmer who moved from MFC to C# and Java over the last decade. I am anxious to see a new approach to C++ coding.

    Reply
  8. John Schroedl

    Like the others, I’m eager to read about your new approach. I use C++, C++/CLI and C# daily in our desktop app but primarily C++ and I welcome any love C++ can get.

    Reply
  9. Richard

    This sounds great for people like me who defected from Win32 and C programming years ago across to Java, and want to generate modern looking FAST code for the Windows platform again.

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s