Heater
Posts: 17389
Joined: Tue Jul 17, 2012 3:02 pm

Re: Simple example of class and object

Wed Dec 11, 2019 12:54 pm

abhi143,
Have you ever used inheritance for real time project? and if yes then what was that ?
Yes. Industrial control system.
Do you really think inheritance was really necessary for the project?
Not so necessary that the job could not have been done with out it. One can go a long way creating an object oriented design in good old C. The support for classes and templates in C++ makes thing a lot neater and less error prone though.
Why did you think that inheritance is the right option for the project. ?
It was necessary in that a library we used for creating ASN.1 messages for SNMP was in C++. We could have used an alternative but that would have been a lot more work.

But... now you have confused things even more by asking about "real time" when your opening post was about "embedded programming". These are not the same thing at all. An embedded system may well not have any strict real-time requirements. A non-embedded program may be critically dependent on timing, think games and high speed trading.

In a fast real-time system one would be wise to be wary of many features of C++ because of the potential for cause problems with timing determinism. For example: new/delete, virtual method dispatch, exceptions.

On the other hand if you know what you are doing C++ will produce as good a system a C. In fact I once made an example of the same functionality in C++ and C. The C++ version had classes and instantiated many objects. The C version achieved the same using an extra pointer to struct as a parameter to the "methods". The generated code was byte for byte the same for both!

See here: https://www.embedded.com/modern-c-in-em ... d-reality/ for some interesting reading about using C++ in embedded systems.
Memory in C++ is a leaky abstraction .

LdB
Posts: 1670
Joined: Wed Dec 07, 2016 2:29 pm

Re: Simple example of class and object

Wed Dec 11, 2019 2:41 pm

abhi143 wrote:
Wed Dec 11, 2019 9:56 am
Have you ever used inheritance for real time project? and if yes then what was that ?
Do you really think inheritance was really necessary for the project?
Why did you think that inheritance is the right option for the project. ?
Nope and that has nothing to do with C++ it is because it is rare for a C++ compiler to exist for embedded MCU's unless you are willing to pay big bucks for a 3rd party. The vendors generally provide ASM and C and that is about it.
Off the top of my head I can't think of any of the top 10 vendors on 8/16/24 bit MCU's that actually have C++ available.
Number 1 being NXP might but I seem to recall you can't actually use their IDE with C++.
You basically have to get to 32bit MCU before probably half have C++ available but that also tends to be those that simply license cores from companies like ARM.

C++ is available on the Pi so it is a valid option and what you are interested in, but that is far from the norm.

As per what you already seem to know the embedded market is dominated by RTOS systems in volume and sales. The baremetal IOT/Audrino would be next and then after that probably RTlinux.
If you want to look at the market in 2019 this is probably a reasonable place to start and covers most stuff
https://www.embedded.com/wp-content/upl ... _Study.pdf

Again you run into the problem only a few of the RTOS are C++ most are just generic C.

So part of the problem you will have in searching your question is it just isn't that common. That isn't a comment on using C++ it is the luck of having the support on the MCU and RTOS with C++.

Heater gave the basic answer above you might use it if it was available and the other software you wanted to use had it available.

Heater
Posts: 17389
Joined: Tue Jul 17, 2012 3:02 pm

Re: Simple example of class and object

Wed Dec 11, 2019 9:13 pm

LdB,

That is true, assuming we equate "real-time" with "embedded" with micro-contollers, there is not much C++ about until you get to the 32 bitters.

With one notable exception, the 8 bit Microchip AVRs.

Of course you are not going to get the C++ standard library with that and things like exceptions are missing.
Memory in C++ is a leaky abstraction .

Return to “C/C++”