Why move to C++?
As I am on vacation this week, I thought that I would leave behind a short note, which might provoke some discussion.
The most widely used programming language for embedded systems continues to be C. I observe great interest in the use of C++ and have frequently written and talked about the subject. Its adoption is also progressing steadily, though much more slowly than anticipated a decade ago. With this in mind, I would like to pose two key questions:
Why would you move to C++?
- Is it because you want to use object oriented coding techniques?
- Do you just want a “better C”?
- Do you want to be able to leverage class libraries?
- Do you think that the language results in more manageable code for larger projects?
- Do you just want to be up to date?
- Or something else …
Why would you not want to move to C++?
- Is it because you are just happy enough with C?
- Have you hear too many horror stories?
- Are you worried about resource [memory] utilization?
- Are you concerned about legacy code?
- Or something else …
I would be pleased to receive any input via email or comment. Thanks.
I use C++ on embedded systems for greater flexibility in programming and especially for the safety features it enables like RAII, which virtually eliminate memory and other resource leaks. Add to that encapsulation and data hiding (possible in C, easier in C++), object-oriented techniques, and generic programming (omitted from your list but quite handy for optimizing for speed), and you’ve got some good reasons to consider it.
Let’s not be Procrustean — C++ isn’t right for every project, but it’s worth consideration for most substantial ones. After all “embedded” is a broad category, ranging from a simple sprinkler system to a smart phone to a multiprocessor single board computer running Windows 7 or full-up Linux.
Absolutely. Where C++ fits, it can be a good choice. On a small system or a simple application, it is likely to be overkill, at best.
A few of the reasons I picked up an old C++ book, sat down, and learned the fundamentals are:
1. My curiosity about this brave new world called “object oriented programming”, having seen bits and pieces of it here and there
2. It is listed in quite a few job descriptions! I can’t tell you how many times a good familiarity with C++ was a requirement, even for what would be otherwise mild embedded systems jobs
3. It allows another way of looking at problems (say for example encapulation, mentioned by Matthew) and hence
Unfortunately unless I’m moving to more sophisticated platforms I won’t have the opportunity to use it much (due to limited compiler support for lots of popular devices).
You are right Marty, C++ only makes sense if you have a reasonably high power [probably 32-bit] CPU and a sensible amount of memory.
I should have noted that interestingly enough, one compiler actually supports C++ for midrange 8-bit Microchip PICs (PIC16, PIC18F, etc.)
That is SourceBoost C++. I thought that was interesting!
That does sound interesting. I’ll have to look into it.