开发者

C to C++ : Transitioning from one language to the other [duplicate]

开发者 https://www.devze.com 2023-02-06 21:02 出处:网络
This question already has answe开发者_如何学编程rs here: Closed 12 years ago. Possible Duplicate:
This question already has answe开发者_如何学编程rs here: Closed 12 years ago.

Possible Duplicate:

C++ tutorial for experienced C programmer.

I program in a number of languages frequently and have been using C++ lately. Basically my classes are just wrappers around pure C code. Almost like a struct with associated methods. This gives me the encapsulation and privacy that I want for my data. I have a small hierarchy of classes and am just barely using inheritance.

I am familiar with OO concepts and know what search terms to use when i need to find out about a particular concept in this regard. However, as I have discovered in my foray in the programming world, often the language features that are really helpful are hidden to the newcomer or novice, and the useful bits that I need have already been written and are in a library somewhere that is freely available (most times part of the framework - like in .NET).

What path would you suggest to gain this vital knowledge in C++ and stop myself reinventing the wheel (poorly).


This is the wrong way to use C++. You would be better served grabbing a copy of Accelerated C++ and reading it. Yes, it's a beginner book but unless you want to continue treating C++ as just C with objects then you need to focus on how C++ programmers do things instead of just sticking with what you already know. You need to start from the beginning and build a good foundation in C++.


Learn the STL if you're going to really plan to use C++ in the future. Though opinions will vary widely, particularly among the die-hards, I think there is absolutely no problem using C++ as "C with objects."

Boost is also pretty awesome.

EDIT: Note the downvotes already coming in from the die-hards. C++ acolytes really don't like to hear people advocate using the language as "C with objects." I stand by my statement. You can write quite shippable and commercially viable code without going crazy with an RTTI-enabled, templatized, multiply-inherited set of classes. Remember KISS.


Scott Meyers' books are a great place for C programer to begin with C++.


I recommend Thinking In C++ by Bruce Eckle. Normally it's available for free online, or as a book.


I suggest you read the books:

"C++ Coding Standards: 101 Rules, Guidelines and Best Practices" --Sutter & Alexandrescu

"Modern C++ Design: Generic Programming and Design Patterns Applied" --Alexandrescu

And probably anything else by Andrei Alexandrescu that you can get your hands on.

Then, there are a number of design patterns and programming idioms that make it very clear why "C with objects" is extremely reductionist. Just to name a few: RAII (Resource Allocation Is Initialization), PImpl (or Cheshire Cat), Factory functions, Smart Pointers, Singleton, Type Traits, Expression Templates, etc. When you know about these, you are no longer programming in C++, but in ++C (because you get a result that actually reflects the increment over C).

As for not reinventing the wheel, like many have said already, make sure to first explore the possibilities in the Standard Template Library (STL) (which is much richer than you might think) and then look at Boost (www.boost.org) which has libraries for a lot of diverse purposes and they are extremely high quality (and some are just works of art, like Spirit, Proto, Lambda and MPL). After that, there is of course a large amount of open-source software in C++ out there, but use it with caution: sometimes it is better to reinvent a wheel that fits perfectly to your application than to use one that might not be appropriate or powerful enough, or worse, full of bugs!


I suggest the book The C++ Programming Language for filling in the gaps in your basic C++ knowledge, and BOOST as the first place to look for existing libraries supporting your programming.


Have you thought about getting the C++ Primer Plus? It's a really good book.


The C++ Standard Library: A Tutorial and Reference


Read Meyers for specific tips, but also Stroustrup's Design and Evolution. The latter gets into the motivation as to why C++ is what it is, and very much comes from a "how to improve C" viewpoint. As for "The C++ Programming Language", 3rd edition is very long. If you can find the 2nd edition, its much more digestible, although of course occasionally out of date (but mostly just less complete).

0

精彩评论

暂无评论...
验证码 换一张
取 消