I've heard about both before but recently I was surprised by the opinions of Linus Trovalds. |
It's worth noting that when Torvards wrote that post some his opinion likely originated from a time when the C++ standard was new (or in development) and poorly supported by the major compilers. Back then there was also a lot of hype surrounding C++, so I'm willing to bet we was experiencing a bit of hyper backlash, like when everyone tells you to watch a show and that makes you not want to watch it.
Also, some of his complaints were less about C++ itself and more about novice programmers, which back then there were a lot learning C++, just like how today there are a lot learning JavaScript
(As I write this, I realize that my opinion of JS is not too different from Torvald's opinion of C++.)
It's much simpler than C++ and it's completely close to hardware making it a low level programming language. |
Both C and C++ are high level languages. C++ is capable of going as close to the metal as C.
C++, on the other side, I think, uses the advantages of OOP, has better speed resulting in better performance |
C++ code in general does not perform better or worse than C. Although it's subjective, well-designed C++ can perform as well as C while being easier to comprehend at a glance, even if internally what the code may actually be doing may be quite complex).
Briefly, C is like a traditional typewriter while C++ is an electric typewriter. |
I prefer to think of C as a Ford T, while C++ is a modern car. You can more or less accomplish the same (go from A to B) with either one, but all other things being equal the latter is safer and more reliable. Yes, a modern car is more complex, but that's because it has more features. We can argue about whether those features are worth the added complexity, but in the end that's a matter of opinion.
Here's an example of how using C++ helps you write safer code. In C this is a common pattern:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
void foo(){
lock_mutex(&mutex);
//...
if (bar){
unlock_mutex(&mutex);
return;
}
//...
if (baz){
unlock_mutex(&mutex);
return;
}
//...
unlock_mutex(&mutex);
}
|
This code is pretty fragile. If a maintainer ever comes around and adds a new exit point, they must remember to unlock the mutex, otherwise the program may deadlock. In actuality, this is just a form of resource management, and it's similar to malloc()-free() pairs.
In C++ you would do this instead:
1 2 3 4 5 6 7 8 9 10
|
void foo(){
std::lock_guard<std::mutex> lg(mutex);
//...
if (bar)
return;
//...
if (baz)
return;
//...
}
|
The compiler takes care of properly unlock the mutex when lg goes of of scope (when foo() returns). Even if new exit points are added, even if an exception is thrown, mutex is guaranteed by the language to be properly unlocked once the function exits.
In C, every time you create a new resource you have to ensure that you're managing it correctly. In C++, you have the ability to design your classes such that you only need to get resource management right once.