??? 07/12/12 09:51 Read: times |
#187913 - class vs source Responding to: ???'s previous message |
Per Westermark said:
It totally ignores the differences where C++ gives advantages at zero cost to the target hardware.
Namespaces (explicit, or use of classes - especially with static members and zero instanced object, or local variables inside blocks) are always good to have. We want locality of reference and as small global name pool as possible. I find that I use individual source files and variables that are file static (global to the source file, invisible elsewhere) as a way to emulate C++ classes in C. I tend to use accessor methods instead of letting those file static variables be globals. This way, I don't run into collisions with having several variables in sources share the same name. Having a class would make this a lot cleaner. An even better argument for classes would be the case where you have a "driver" for a UART, and your particular micro has more than one UART. If you really want to use one source file (say, uart.c) to handle multiple hardware UARTs, you at minimum need to keep a structure which has the addresses of the UART's data registers, and perhaps other stuff. It gets even more complicated if you implement software FIFOs which are interrupt driven. You get into a hell of pointer dereferencing and which ISR is which and yikes. A class would abstract all of that. I'm not convinced the overhead of a class (in terms of address passing behind your back) is any better or worse than doing it yourself, but I'd rather let the compiler handle it. I don't use dynamic memory allocation (malloc) so using new and delete wouldn't be an advantage for me. -a |
Topic | Author | Date |
Who the hell uses C++ in embedded systems... | 01/01/70 00:00 | |
So? | 01/01/70 00:00 | |
beware of (verbal) inflation. | 01/01/70 00:00 | |
Not as many as some would think. | 01/01/70 00:00 | |
2% don't know | 01/01/70 00:00 | |
Same same | 01/01/70 00:00 | |
Surveys | 01/01/70 00:00 | |
Me! | 01/01/70 00:00 | |
Using tools that only supports C? | 01/01/70 00:00 | |
Tools and MISRA | 01/01/70 00:00 | |
C++ "better than C" for embedded systems | 01/01/70 00:00 | |
as far as the '51 goes | 01/01/70 00:00 | |
As far as '51 goes - C++ is still viable | 01/01/70 00:00 | |
class vs source | 01/01/70 00:00 | |
I did say "smallish" | 01/01/70 00:00 | |
Overkill? | 01/01/70 00:00 |