可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
For those of you with experience with both, what are the major differences? For a newcomer to either, which would be better to learn? Are there situations where you might choose C but then other situations where you would choose C++? Is it a case of use the best tool for the job or one is significantly better than the other. I know C++ is an "enhancement" of C, but it was created in '83 and hasn't completely replaced C so there must be something more to it.
I know this question is subjective and I am not trying to start any religious war, so please try to be as objective as possible. Clear strengths and weaknesses and comparisons.
回答1:
While C is a pure procedural language, C++ is a multi-paradigm language. It supports
- Generic programming: Allowing to write code once, and use it with different data-structures.
- Meta programming: Allowing to utilize templates to generate efficient code at compile time.
- Inspection: Allows to inspect certain properties at compile time: What type does an expression have? How many parameters does a function have? What type does each one have?
- Object oriented programming: Allowing the programmer to program object oriented, with sophisticated features such as multiple inheritance and private inheritance.
- Procedural programming: Allows the programmer to put functions free of any classes. Combined with advanced features such as ADL allows writing clean code decoupled from specifics of certain classes.
Apart from those, C++ has largely kept compatibility with C code, but there are some differences. Those can be read about in Annex D of the C++ Standard, together with reasons and possible fixed to make C code valid C++ code.
回答2:
C++ is 99% a superset of C. It's a little more strict in syntax, with a few very minute differences in terms of things changing.
The biggest difference is that C++ makes an attempt at being object oriented. There's native support for classes.
There's a few other perks in C++: templates, stream operators, pass-by-reference (a bit less confusing than pass-by-pointer)
What do you lose out for going C++? It's missing some of the lowest-level hacks that a lot of people use C for. I don't remember any of them offhand, but I've never heard any good argument for tricking the compiler into doing what you want except as a way to push efficiency that extra 10%.
回答3:
C++ is, as it's name implies and as you said in your question, an enhancement of C. It's a significant enhancement. (And I use the term 'enhancement' to refer to features, not to function.) The thing about enhancing, though, is that it means growth. C++ typically lends itself to much bigger programs. Applications, really. It's a high performance language, but it's big.
C, on the other hand, is used for kernel and driver programming for a reason. It's old (ancient?), small, and if you're smart, about as fast as you can get without writing assembler yourself. The tradeoff, obviously, is features. C doesn't have a lot of the nice big squishy concepts like classes and templates that C++ programmers like myself take for granted (yep, totally guilty).
So to answer your question more directly, most of my large, high performance projects get written in C++. If I'm working on something like a driver or an embedded system, I'll expect to be using C.
回答4:
If you've never used a language that requires you to do manual memory management I would go for C first.
Concentrate on the C fundamentals like strings, function pointers, and how memory is used and managed. These will all transfer when you make the transition to C++. Above all else, make sure you really grok pointers, how they relate to memory, and the relationship between pointers and arrays. I would say to be a well-rounded programmer understanding these things is required.
Then, go to C++ and learn about the OO model, templates, etc. Trying to do everything in C++ from the very beginning can be a little overwhelming.
回答5:
I would make the argument that you would be better off using C++ over C in most cases. You don't have to use all of the complicated features of C++ if you don't want to. There are a few things that C++ add that are really helpful for most cases:
- Stronger typing.
- A string class included in the standard library.
- An array class (vector) that grows as you need it to and handles all of the allocation and deallocation of memory for you.
Personally, I feel that those three things make using C++ worth it even if you use it to write C-like code (aka procedural, non object-oriented code).
Maybe if you're doing some kernel hacking or embedded systems development you should use C, but otherwise, I'd recommend C++.
回答6:
For those of you with experience with both, what are the major differences?
C is a subset, C++ is a superset. C++ includes features to support object-oriented programming (e.g. "polymorphism"), and many other features.
For a newcomer to either, which would be better to learn?
C is easier (because it's a smaller topic), and C++ is better (because it's more powerful, includes C, and in my experience there are more jobs programming in C++ than there are in C).
Are there situations where you might choose C but then other situations where you would choose C++?
I'd choose C over C++ in the rare, rare situations where the target platform supports C but not C++ (i.e. on some embedded devices).
回答7:
You use C++ where you can and C where you have to. Generally speaking, if you have a C++ compiler available for your platform, there's no reason not to use that. C is a perfectly good language but C++ adds so much extra without losing you any power, so it would almost always be the language of choice.