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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

From C to C++: A quick reference for aging programmers

82 点作者 angelortega超过 13 年前

16 条评论

exDM69超过 13 年前
No no no no. This is not a very good reference or tutorial or even an introduction. It even teaches some bad practices and omits crucial detail.<p>The biggest problem I saw was that it introduces classes and objects but does not tell you anything about the "rule of 3", destructors, copy constructors and assignment operators. These are crucial to understanding C++'s pass-by-value semantics and writing sensible C++. C++11 adds to this mess by adding rvalue references, move constructors and whatnot. Writing good and safe C++ requires you to use stack based resource management wisely and use ctors/dtors for initialization and cleanup (this is also why you almost never write try/catch in C++ - and there's no finally statement).<p>Another example: the article shows you how to do a Java-like constructor like this: point::point(int x, int y) { this-&#62;x = x; this-&#62;y = y; }<p>When in C++ you want to use initializer lists like this: point::point(int x, int y) : x(x), y(y) {}<p>At best this tutorial teaches you to write "C/C++" (the worlds most popular programming language that does not exist) or "C with classes". This style of programming has all the disadvantages of writing C but none of the advantages of C++.<p>C++ is one of the most complicated languages out there, on par with Haskell and Scala. It's C legacy makes it also somewhat dangerous and the pass-by-value semantics make it very different from any other language out there. You cannot expect to learn even a little bit of C++ from little vague blog posts like this.<p>If you want to learn C++, buy a book! The web is full of crappy resources like this one.
评论 #3114577 未加载
评论 #3114536 未加载
评论 #3114567 未加载
obiterdictum超过 13 年前
This feels like a C++ tutorial from mid-90s. C++ code written today looks nothing like what's presented in this article. Besides, some poor sample code choices jump out at me, like throwing an int and using C library. I realize they are there to illustrate a point, but this reinforces bad habits.<p>In fact, if you are learning C++, you better start it as a new language, rather than a tumor grown on the side of C. These languages are too idiomatically different.
评论 #3114861 未加载
AshleysBrain超过 13 年前
Seems to be missing some important points:<p>const methods - if you declare a <i>const rectangle r</i>, you can't call <i>r.surface()</i> unless it's declared <i>int surface(void) const {...</i>.<p>references - article seems to be confused why they exist. They enable some extra handy features, e.g. you can pass a temporary to a function parameter taking a const reference. e.g. you can pass f(MyClass()) if it's declared void f(const MyClass&#38;), but not if it's declared void f(const MyClass*).<p>STL - things like std::string, std::vector, std::map - totally invaluable!<p>And lastly, the fact that programming in C++ is completely different to C. It's not like C with bolted on features. It's a whole new paradigm. For example, in modern C++, you very rarely have to manage memory. Smart pointers and containers can generally manage it all for you.
js2超过 13 年前
Here's a first aid kit for when you shoot yourself in the foot with this quick ref: <a href="http://www.parashift.com/c++-faq-lite/" rel="nofollow">http://www.parashift.com/c++-faq-lite/</a><p>Also grab a copy of effective c++ by scott meyers.
评论 #3114797 未加载
评论 #3115385 未加载
评论 #3114705 未加载
5hoom超过 13 年前
Beautifully concise!<p>That would normally be about a chapter per-paragraph in most C++ resources around. Well done :)
DCoder超过 13 年前
<p><pre><code> template &#60;class ttype&#62; ttype multiply(ttype a, ttype b) { return a * b; } </code></pre> int * int doesn't always fit in an int. C++ Templates: The Definitive Guide suggests using a traits class to solve problems like this. Templates are a complex subject in general, and I wouldn't recommend using them without having read the aforementioned book.
评论 #3115780 未加载
Hitchhiker超过 13 年前
I can feel a flame war and potential crusade here. Being the imps that some of us are, how can we pass up this potential opportunity for enjoyment :<p><a href="http://lwn.net/Articles/249460/" rel="nofollow">http://lwn.net/Articles/249460/</a><p>p.s. best comment in defense of the original poster so far has been by Shin Lao.<p>p.p.s the tut was targeted for a very specific audience + purpose. It was not purporting to be Bjarne Stroustrup's next meditation. Folks can offer support to another community member _ without _ being mean.
评论 #3114613 未加载
Hitchhiker超过 13 年前
At 29, I may not be aging. But this is beautifully clear. Like fine chocolate, coffee or poetry. Good stuff.
tripzilch超过 13 年前
This is superb. <i>Exactly</i> the quick-n-dirty sort of intro to C++ I have been looking for for <i>years</i>, it seems.<p>Kudos!
etanol超过 13 年前
«And another thing you always desired: the struct keyword is not needed in the declaration»<p>Someone doesn't understand the value of the "struct" keyword:<p><a href="https://github.com/torvalds/linux/blob/master/Documentation/CodingStyle#L272" rel="nofollow">https://github.com/torvalds/linux/blob/master/Documentation/...</a>
评论 #3115004 未加载
Hitchhiker超过 13 年前
@ Ángel , come to think of it , you may be one of the rare souls here who may smile at this from the heart:<p><a href="http://news.ycombinator.com/item?id=3114374" rel="nofollow">http://news.ycombinator.com/item?id=3114374</a><p>I second 5hoom's additional attribute that you have demonstrated.
dbattaglia超过 13 年前
Nice quick-and-dirty guide. The only thing I personally would have added more emphasis on is explaining (via code examples) the actual use case of virtual methods: storing instances of derived objects in a base class pointer/reference, while still calling the correct runtime type's method.
KonradKlause超过 13 年前
The author seems not to know that in C++ structs and classes are the same. The only difference betweem them is that in classes the default acccess mode is private and for structs it's public.
评论 #3114603 未加载
LostInTheWoods2超过 13 年前
"A quick reference for aging programmers" .. anyone else offended? Maybe someone should write a post titled "From C++ to C: A quick reference for young programmers who think they're clever"
forgotAgain超过 13 年前
Why "aging programmers" and not just "C programmers"?
X4超过 13 年前
Wow.. if I were an aging C programmer I would laugh about C++, Ruby, Java programmers