views:

388

answers:

7

I want to learn C++ for windows but have no idea where to start; there seem to be so many different technologies that have utterly confused me: Win32, MFC, ATL, STL etc. I know a good deal of C# and Java, no C and a small amount of ANSI C++ (datatypes, loops, classes and OOP), but when I look at other people's code or try to mess around with an MFC project in VS2008, it makes no sense.

So; how should I start? Can you recommend any good books and what do I actually need to learn?

+2  A: 

If you want really learn C++ for windows ... Have a look at Qt !

Matthieu
What for? How does that help him learn C++?
jalf
I recommand Qt instead of MFC !
Matthieu
@jalf: "Qt uses C++ with several non-standard extensions..." - Qt is written in C++.
Callum Rogers
+1  A: 

Start off with basic C Programming that also includes pointers. The Win32 API, MFC etc revolves more around with pointers, so you need to have a good grasp of pointer concepts. Initially begin with Charles Petzold's Windows Programming or Herbert Schildt's Windows Programming from the Ground up and then move to MFC,ATL etc

Ram
if you want to learn C++, C is pretty much a waste of time.
jalf
@jalf No, it isn't. C is much simpler than C++, but still allows the programmer to learn some important stuff like pointers. In C++, many things become way more complex and the way some things work(like strings) is obfuscated.
luiscubal
+2  A: 

this is not necessarily for windows, but there is a very good c++ tutorial here: cplusplus.com

clamp
+2  A: 

If you know C#, then you may want to start with Managed C++, which includes Windows.Forms, WPF and all those APIs you may be used to.

If you want something new, I'd suggest you should learn plain C first(including pointers!) and then learn Win32 API. Once you know Win32 API you can move to other APIs.

luiscubal
I actually started on Managed C++ and moved to C#, but there is no point in going back to it when C# can do the same things but is easier to code.
Callum Rogers
+2  A: 

See: How to quickly get started with Visual C++?

Pukku
+4  A: 

Start with the language. The first thing you want to do is learn C++. Then you can bolt the Windows API on top of that afterwards (this is partly to let you focus on one thing at a time, and partly because the Windows API is so horrifically badly designed)

So learn the C++ language. Part of this is learning to use the C++ standard library, and a big part of this is the STL.

Other than that, make sure you understand pointers, templates, and C++'s flavor of OOP (in particular, the relationship between constructor, copy constructor, assignment operator and destructor, the combination of which give you a lot of freedom to design classes that behave exactly like you want --- or give you a lot of freedom to screw up). The latter should lead you to understand RAII, a hugely important tool for resource management and for avoiding leaks.

Try to do as much as possible in the core language. Write small text-based apps, so you don't have to use external libraries yet. If you want, experimenting with the Boost libraries can be an eye-opener (and it will test your knowledge of the C++ language), so once you feel ready to go beyond the core language, this might be the first library you'll want to play around with.

Once you really know C++, go ahead and look into the Win32 API. It's ugly, inconsistent and overcomplicated, but it's hard to program C++ on Windows without it. Unfortunately.

MFC can be mostly ignored. It was an attempt to make an OOP abstraction over (much of) the Win32 API, but it's not particularly nice to work with, and if this is what you want, you'll probably be better off with .NET.

ATL is another attempt at wrapping some of the worst bits of the Win32 API, this time using more modern templates and generic programming. I haven't used it much, but it's supposed to be a huge improvement over MFC at least. But it's still another layer of abstraction, and another dependency which might make it harder to get up and running and actually understand your code.

So for now, I'd say ignore MFC and ATL. If it turns out you need one of them, learn them, but start with the core Win32 API. After you've learned C++ (including the STL).

And a few final notes: Don't get C and C++ mixed up. Treat them a as different languages. Just because some C code will compiled as C++ doesn't mean it's a good idea. Good C code is usually horrible C++ code (if it compiles as C++ at all), and good C++ code definitely won't compile under a C compiler. Each language has its own idioms and best practices, and they are miles apart.

Which is also why I wouldn't encourage you to learn C before C++. There's no point. Either you learn a lot you won't really need afterwards, or you learn a lot which you then mix up with C++ and end up with a fundamentally broken and error-prone C/C++ hybrid language. (Which is probably the default case for C programmers learning C++. They don't. They learn a few of the new constructs, and then treat it as "C with classes")

About what to read, and how to learn the language, there are a couple of things:

C++ is a notoriously underspecified language (and before the C++ fanboy brigade comes along to argue, I mean underspecified as in "it leaves a lot unspecified", not necessarily "it should specify more"). There are countless instances of simple, harmless-looking code, code that actually works fine when you compile and run it, but which is undefined behavior, meaning that the C++ standard says nothing about what it should do, and it's allowed to do anything, including formatting your harddrive, make demons fly out your nose... or just seem to work... today... on your computer.

This means that you should:

  • Be paranoid about your code. Don't trust it to be correct just because it compiles, or just because it gave the right result when you tested it. In C++, "try it, and see if it works" is not a valid answer to questions. ;) (And if you look at C++ questions here on SO, you'll find that they often take a very special form, where every answer is questioned in the comments, and is only trusted if it can reference the relevant part of the standard)
  • Get used to having a copy of the standard nearby (you can buy the ISO standard in pdf format online, or you can find free draft versions on google that are pretty much just as good), and get used to looking things up in it. It's not exactly easy reading, and you'll probably need a bit of experience with the language before you're able to make sense of it, but in the long run, it's definitely a good idea.
  • As an extension of the above, trust no one else. Some books good too, but many are unfortunately written by authors who didn't have the required C++ skills, and either teach you bad habits, or even rely on undefined behavior in their sample code. This goes doubly for online tutorials. They are almost always bad for you.

As for which books to read, it's probably easiest to mention a few authors you can trust: Scott Meyers, Herb Sutter, Andrei Alexandrescu, Bjarne Stroustrup have all written some good books, and definitely know their stuff. Stroustrup's "The C++ Programming language" in particular is a very good introduction to the language. I've probably missed out a lot of good books by knowledgeable authors, but these are the ones that came to mind, and you can't really go much wrong if you pick a random one of their books. ;)

jalf
Great answer! Thank you!
Callum Rogers
+1  A: 

I want to learn C++ for windows but have no idea where to start; there seem to be so many different technologies that have utterly confused me: Win32, MFC, ATL, STL

Since everything eventually comes back to Win32 that is where I would start. With a good grounding in Win32 things like MFC and ATL become much easier to understand (Note: STL is C++ specific and is not Windows specific).

I would recommend downloading the Borland Win32 SDK help file. It is a little dated but it still contains a lot of relevant Win32 information.

I would also recommend working your way through the Windows API Tutorial. It does a nice job of explaining how to write Win32 wrapped in C++ layer.

jussij