TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Learning C++ in 2010

38 点作者 martinp超过 15 年前
I'm currently learning C++ for a project I'm working on. However, I do have a problem. The huge amount of (possibly incorrect) information Google returns leaves me a bit confused sometimes. Like PHP and many other languages, there's obviously a lot of bad code out there. So I'm wondering what are the best resources for learning C++ in 2010? Has the best practices for C++ changed much in the last 10 years? The most valuable resource I have found so far is the Accelerated C++ book, is it still relevant?

28 条评论

algolicious超过 15 年前
Effective C++ by Scott Meyers has some good tips.<p>Here are my tips for you. First tip: Don't trust any C++ code that anyone else writes. This is related to Meyers' tip that C++ is a "federation" of languages. As you have noticed, there is a lot of bad code. But even good code can have bad consequences. C++ is extremely idiosyncratic and the author will make assumptions that you will follow certain practices (deemed as "the right way to program in C++") which will vary from author to author. You may even start to notice this in the comments. Corollary: Only trust a library if it's very well documented and frequently used. Boost may be worth a look, and lots of people like it, but I can't promise that it works.<p>Second tip: avoid reference counting "smart pointers" whenever possible. This directly contradicts Scott Meyers. Instead, use valgrind to make sure your program doesn't leak memory. If your program is infeasible to write without automatic memory management, switch to a language such as Java or C# which is built atop a robust garbage collector.<p>Third tip: view C++ as a bit like an extremely sharp knife. Easy to get things done, even easier to cut your finger off. Just because an example looks slick and easy does not mean that using it in practice is slick and easy.
评论 #1121300 未加载
评论 #1121458 未加载
评论 #1120617 未加载
palish超过 15 年前
Go to the IRC chatroom #gamedev on the server irc.afternet.org. Ask specific questions, and put up with people being frustrated by your lack of knowledge. You can quickly get the correct answer to almost any C++ question you can think of, but you have to put up with a lot of silly flaming / chest beating to get to the answer.<p>Seriously. I am a C++ expert (10 years and counting) and I've never read a single book on C++ in my life. Just keep programming and keep asking questions until you get answers.<p>Ignore fads / popular opinion if you think you know a way of coding something in a simpler, cleaner way than what most people think you <i>should</i> be doing for some silly pedantic reason. (If they have a point, then use their technique of course -- but if it's like "All objects should be declared as noncopyable!" then ignore it. 99% of the time, objects should be considered not-safe-to-copy, so there's no reason to bloat the code by declaring objects as noncopyable. That type of thing.)<p>Also, <i>AND I KNOW I'M GOING TO GET FLAMED FOR THIS,</i> try to avoid using Boost unless you're doing multithreading. Just because you <i>can</i> use smart pointers, doesn't mean you need to use them <i>everywhere</i> and just because you feel like you "should be". Boost seriously bloats the code (compilation times shoot <i>way</i> up, and there is runtime overhead as well, not to mention that if you want to send your code to someone then they need to have Boost installed to even compile it). I don't use smart pointers at all, and my code doesn't crash or have memory leaks. It's not hard to write a manager class that allocates a type of object and adds it to an internal list, then de-allocates all of the objects when the manager destructs.<p>Write a program in the simplest possible way you can think of to get it working correctly. Then re-write your code entirely, so that it's more readable and cleaner (and commented). Format the code using tabs / whitespace so that the code is even more readable. Then re-write it again, this time simplifying the code even further. Now you have clean, well-written C++ code.
tmsh超过 15 年前
There's a somewhat recent reddit thread, fyi.<p><a href="http://bit.ly/bTMjtd" rel="nofollow">http://bit.ly/bTMjtd</a><p>I've found the best practices for C++ are usually to practice avoiding best practices whenever possible. That is if you're in it for the olde 10-year Norvig cycle.<p>Time allowing, bottom-up programming (as the equally venerable On Lisp details) also applies to design patterns, template meta-programming and all that. It's good to read about them, to learn what others have experimented with and found to be useful (or so they think). But you're only really going to learn them in the context of something useful and practical. It's much better to discover them for yourself in practice.<p>That said, there's a lot of C++ syntax and STL idioms that are not obvious at all -- which make Meyers' books very important (particularly the first one) even for starting out.<p>Also, C++ as doctrine is not fun -- i.e., a way of 'thinking' about programming. But C++ as a collection of language-changing extensions to C is not so terrible. I guess that's true for anything.<p>Actually, now that I think about it. There's Lisp and C -- elegant, simple languages on top of assembly and the lambda calculus if you wish or FP if that's what you emphasize (i.e., math). These are in fairly close approximation to code (assembly) that is in fairly close approximation to how hardware or math works. These provide structure on top of that.<p>And the rest, whether OO in Java and C++ and its rough attempt to merge type theory with performance-consciousness of the actor model from Simula and Smalltalk and Objective-C and all that -- the rest are just organizing paradigms on top.<p>It's late. I've been coding all night. I forget what the question is. There's like 10 dimensions in play when you learn a new language. But like anything, if you're in it for the long term, whatever you learn, learn it really well. There's a lot of similarities at all levels.<p>tl;dr Read SICP.
unwind超过 15 年前
Nowadays, many people think Boost (<a href="http://www.boost.org/" rel="nofollow">http://www.boost.org/</a>) when someone mentions C++.<p>Feel free to look through Stack Overflow's C++ tag (<a href="http://stackoverflow.com/questions/tagged/c%2b%2b" rel="nofollow">http://stackoverflow.com/questions/tagged/c%2b%2b</a>) to get a flavor of this, many C++ questions have a Boost answer.
ashu超过 15 年前
<a href="http://www.parashift.com/c++-faq-lite/" rel="nofollow">http://www.parashift.com/c++-faq-lite/</a> -- one of the best resources for c++, imho
评论 #1120514 未加载
Maro超过 15 年前
Whatever you do, don't look at Bjarne Stroustrup's book, it's terrible. Ignore Andrei Alexandrescu, he tries to turn you into a template metaprogramming zombie. The way to go is to find a subset of C++ which works for you and stick to that.
评论 #1120221 未加载
评论 #1120182 未加载
评论 #1120157 未加载
评论 #1120421 未加载
phaedrus超过 15 年前
<i>Practical C++</i> by Mark A. Terribile is very dense, but clear, and can take you all the way from novice to expert if you can get through it. The copy I have is a decade and a half old, but still relevant for the material it covers, which is substantial. The thing to understand is rarely does the C++ standards committee remove any <i>major</i> feature of C++, they mostly add or enhance, so an old book can still be relevant if it was a well written book, you just might have to look in other sources for other topics.<p>I thought I knew C++ well, but the book <i>Modern C++ Design</i> by Andrei Alexandrescu completely blew my mind. It's a bit advanced, and more of a cookbook than in introduction, but it does serve to introduce some of the more recent innovations in C++ programming.<p>And of course don't forget stackoverflow.com. Heck, I think I saw this exact question (good C++ programming books) was a top question a couple months ago.
kvs超过 15 年前
I came from C to C++ about 5 years ago. I didn't really use any books for the first few months. I have only used <a href="http://www.parashift.com/c++-faq-lite/" rel="nofollow">http://www.parashift.com/c++-faq-lite/</a> a lot during this period.<p>Soon I realized C++ is full of idioms that you need to be aware of most of them to be really good-- which basically meant you know exactly what you're asking the compiler to do. For that, I have used Effective C++ (don't forget -Weffc++ flag in GCC), Exceptional C++, Effective STL, The C++ Standard LIbrary (Josuttis) as reference and reading. They have helped a lot.<p>Now, I look into boost source tree once in a while to see how they do certain things or to debug some boost libraries. Nowadays that's where most of my learning comes from.<p>"In C++ it's harder to shoot yourself in the foot, but when you do, you blow off your whole leg." — Bjarne Stroustrup
jey超过 15 年前
Accelerated C++ by Koenig and Moo is a great choice. Stick with it.
Catenacci超过 15 年前
I'd bet you're maintaining legacy C++ code. If so, while it's good to look at good C++ code (like Boost and the materials you can find on the C++ FAQ) chances are you're going to run into some fairly gnarly code--especially if code has been maintained over several years by several developers. Having maintained C++ legacy code in a commercial app for the last 5 or 6 years--I wish I had some good advice to share. There are a few things:<p>1.) May sound obvious but good version control is essential. A seemingly innocent change can have nasty side effects. You need to be able to revert to a known to be good state easily.<p>2.) Watch out for side effects. If something looks odd it may be because there was some very valid reason for the odd construct. Some of the developers working on this commercial app decided to add a copy constructor to a class which never had it before. Ironically, when they added a copy constructor, it trounced all over some bitwise copying code that had been hacked in years before. Hence they had to go back to an older version to straighten things out (see point 1 above).<p>3.) Add intermediate variables to help yourself understand the code. You'll probably see constructs like this:<p>func1()-&#62;func2()-&#62;func3();<p>This means that func1 returns a pointer which is then used to get func2 which returns a pointer which is then used to invoke func3. This is horrid code but it seems to be a very common antipattern in C++ code (at least in my experience). Adding variables to hold intermediate pointers can help a lot in understanding what the code is doing and in checking to insure that you're testing for bad pointers.<p>Those are just some thoughts off the top of my head.
chadaustin超过 15 年前
If you're on Windows, the MSDN Library DVDs that come with Visual Studio are a great resource. They have accurate and comprehensive documentation on the syntax and standard libraries.<p>I've worked with some people who used Google as a C++ reference, but they'd often write code with subtle problems because some random blog or reference isn't a good way to build a solid understanding. I'd suggest going straight to the source: the standard (it's not that complicated), your compiler's documentation, and Guru of the Week: <a href="http://www.gotw.ca/gotw/" rel="nofollow">http://www.gotw.ca/gotw/</a>
评论 #1121143 未加载
tptacek超过 15 年前
You want the Scott Meyers books, particularly Effective C++ and Effective STL (I don't know if they're still in print, but you want them).<p>Effective STL is probably the single most valuable C++ book out there. A decent but superficial grasp of C++ plus Effective STL will get you a long, long way in actual coding.<p>Every C++ dev should have Stroustrop on their desk.<p>If you're just coming to C++, Alexandrescu will just confuse you.<p>Watch out for Boost, the most popular C++ add-on library. It's solid, but it's also confusing, and you don't <i>need</i> it for anything.
pkaler超过 15 年前
Start by learning C. The best way to learn C is by reading The C Programming Language by Kernighan and Ritchie.<p>Then read Effective C++, More Effective C++, and Effective STL by Scott Meyers. Then move on to Exceptional C++, More Exceptional C++, and and Exceptional C++ Style by Herb Sutter.<p>Stay away from Alexandrescu's books. Template meta-programming and "modern C++" is very powerful but will only end in tears. It's great to know this stuff but probably shouldn't be used in production code that you actually want to ship.
评论 #1120040 未加载
评论 #1120062 未加载
评论 #1120224 未加载
Daramarak超过 15 年前
I would look at Thinking in C++ by Bruce Eckel (<a href="http://mindview.net/Books/TICPP/ThinkingInCPP2e.html" rel="nofollow">http://mindview.net/Books/TICPP/ThinkingInCPP2e.html</a>)<p>It is a free resource, and gives you the whys and hows on most aspects of c++. Buy the book when you decide that you like it, I did. It is especially good if you have some prior experience to programming, like java or C<p>And boost is almost an extention to the std libraries, so get to know them, those are tomorrows standards.
评论 #1120231 未加载
mykphyre超过 15 年前
I first learned to program using C++ and found this online resource to be extremely helpful:<p><a href="http://www.cplusplus.com/reference/" rel="nofollow">http://www.cplusplus.com/reference/</a><p>It has great reference for the C++ and C standard libraries. The best thing is that there are simple but _very_ good code examples accompanying each function description showing how to use it in context. Can't recommend this reference highly enough.
hga超过 15 年前
It's very old and I have no idea how accurate it still is, but I found <i>Inside the C++ Object Model</i> by Stanley Lippman (<a href="http://www.amazon.com/Inside-Object-Model-Stanley-Lippman/dp/0201834545" rel="nofollow">http://www.amazon.com/Inside-Object-Model-Stanley-Lippman/dp...</a>) to be <i>tremendously</i> useful in the mid-'90s for understanding what was going on under the hood.<p>Stroustrup's <i>The Design and Evolution of C++</i> (<a href="http://www.amazon.com/Design-Evolution-C-Bjarne-Stroustrup/dp/0201543303/" rel="nofollow">http://www.amazon.com/Design-Evolution-C-Bjarne-Stroustrup/d...</a>) is very good for explaining the "why" of C++, especially the stranger parts.<p>One other note, echoing some of the others: <i>everyone</i> picks out a subset of C++ and programs in that, and smart companies make that formal. You might see if your problem domain matches one of the available good ones, like Google's (well, I've heard that it's good).
CyberFonic超过 15 年前
Sorry about being a bit tangential. Are you working on the project on your own? If so, then how did you choose C++? To me it almost seems like the wrong answer regardless of what the question was.<p>What are your prior programming experiences? E.g. if you know Scheme / Common Lisp you'll approach C++ very differently to someone who is coming from a Java/C# background which again is different for a C background.<p>If you are working with others, then you might want to ask them for their suggestions. And as many have pointed out, it is a rare project that uses all of C++ features, so you'll want to know what the "house style" and conventions are.
vinc456超过 15 年前
I've been working through the Stanford course notes on Programming Abstractions. <a href="http://www.stanford.edu/class/cs106b/" rel="nofollow">http://www.stanford.edu/class/cs106b/</a> Click on the link to the course reader PDF<p>I recommend it because it illustrates important concepts in Computer Science while avoiding the unnecessary idiosyncrasies of C++. I like to think of it as a Coles Notes version of CS essentials but the material might be a little too basic for an experienced programmer.
JamieEi超过 15 年前
I love the Meyers books like most everyone below, but I don't think they are a good place to start learning C++. I'd begin with the 4th ed of C++ Primer, which differs from previous editions in that it uses the standard libraries from beginning so you don't spend 400 pages wading through bad code that you would (hopefully) never write in the real world.
rs超过 15 年前
What do you guys think of "Thinking in C++" by Bruce Eckel ?<p><a href="http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html" rel="nofollow">http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html</a><p>I remember back in college I used to refer to one of his books (can't remember which, I'm afraid) and it was pretty well written
aufreak3超过 15 年前
&#60;sigh&#62; .. I'd like to say "mu", but here's a bit more. If your project has enough idiosyncrasies to demand C++, then <i>you</i> are best placed to attain guru status in that aspect. So don't listen to gurus and build your own "idioms". The language is bloody flexible for a low level one.
评论 #1120329 未加载
stonemetal超过 15 年前
Accelerated C++ was published in 2000 The last C++ standard was in 1998 so it is still relevant to the current standard of the language. The way to learn C++ is to start by treating it like C with a new standard library. Then expand to learn classes and finally templates.
评论 #1120483 未加载
jefffoster超过 15 年前
The GotW archive is freely available at <a href="http://www.gotw.ca/gotw/" rel="nofollow">http://www.gotw.ca/gotw/</a> and is a good resource for realizing just how much information you need to know to understand a small snippet of C++ code!
lambdom超过 15 年前
Effective C++ from Scott Meyers (and the serie: More effective C++, Effective STL) are the first book that showed me the difference between coding and writing good code in C++. I strongly recommand it.
known超过 15 年前
I'd suggest you to study <a href="http://xmlrpc-c.sourceforge.net/" rel="nofollow">http://xmlrpc-c.sourceforge.net/</a> code.
zb超过 15 年前
Anything by Scott Meyers. They're really as good as everyone says.
leif超过 15 年前
Effective C++ is decent.
thedp超过 15 年前
You must checkout Cprogramming.com<p>It's a community dedicated to C and C++.<p>It has great tutorials, references and an amazing forum.<p><a href="http://www.cprogramming.com/tutorial.html" rel="nofollow">http://www.cprogramming.com/tutorial.html</a><p><a href="http://www.cprogramming.com/board.html" rel="nofollow">http://www.cprogramming.com/board.html</a>