Pages: [1] 2   Go Down
Author Topic: C++ in embedded  (Read 2625 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 1
Posts: 153
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all

If I am not wrong the C++ is barely used in embedded
 programming , C is the facto language.

So I want to ask : what is the real advantage of C++ classes
 which is the main difference to C ?

I ask in relation to embedded programming NOT
 desktop programming .

Thanks
Elico
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24370
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
If I am not wrong the C++ is barely used in embedded programming ,
I'd say you are wrong.
You probably have a mobile phone.
That'll almost certainly have a lot of C++ in it.

The Symbian mobile OS is almost exclusively C++, except for very low-level functions.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 121
Posts: 8452
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Although most people write in C on the Arduino in fact lot of the Arduino code is C++.

Used properly C++ is very appropriate for embedded work.

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Offline Offline
Full Member
***
Karma: 1
Posts: 153
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

"So I want to ask : what is the real advantage of C++ classes
 which is the main difference to C ?"

Elico
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24370
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code reuse.
Better abstraction mechanisms.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Belgium
Offline Offline
Edison Member
*
Karma: 58
Posts: 1731
Arduino rocks; but with my plugin it can fly rocking the world ;-)
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code reuse.
To make this very concrete.
Consider the blink without delay example
If this code was written as a class (so C++) you could easily extend the program to blink a led on each and every pin.
With the current code this is not so easy.

Better abstraction mechanisms.
To make this concrete:
I have a big project with plenty of classes. For debugging purposes I log the state of all classes at every loop run.
To do so all classes have the same method called void LogRow(stream str);
There are 3 levels of abstraction here.
The most obvious is that it does not matter what the class is doing (gps logger; motor driver; sensor) you just call classname.LogRow(); and you get your data logged.
The second level of abstraction is in stream. stream can be a sdcard file or it can be the serial monitor or it can be any device connected to a serial pin.
It does not matter. By changing 1 parameter you can have the logging on the sd card (for instance when the robot is outdoors running) or to your serial monitor (when you are trying to understand what is ongoing) or to a other device (Arduino/pi) which does something smart.
The third level is that a class can be derived from another class. In this case the LogRow() will logg its own data and then call LogRow() of the parent class.

I hope this makes things a bit clearer.
Best regards
Jantje
Logged

Do not PM me a question unless you are prepared to pay for consultancy.
Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

Offline Offline
Full Member
***
Karma: 1
Posts: 153
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

CLASSES vs funtctions .

Why this is easier/better then just using regular functions
 that use parameters given to them when called
  and acting accordingly ?
like func1(p1,p2,p3) ;

Elico
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24370
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Because functions are not logically grouped and managed as they are in a class.
Functions cannot easily be instantiated for a variety of related objects, as classes can.
There is no inheritance for vanilla functions.

If you need to learn OOP, then a visit to a book store is better than asking random questions here.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Full Member
***
Karma: 1
Posts: 153
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks

I did not asked you what to do ..

Elico
Logged

New Hampshire
Offline Offline
God Member
*****
Karma: 13
Posts: 779
There are 10 kinds of people, those who know binary, and those who don't.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks

I did not asked you what to do ..

Elico

Perhaps not, but when entire books are written primarily to answer the questions you ask here, then the answer to your questions are, go read the books.
Logged


Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 331
Posts: 16518
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 For me as more hardware oriented person, I USE C++ classes only when I use such library code as the Arduino core functions have implemented. They are not hard to use. As a writer of Arduino sketches I try to ignore as many C++ features as I can get away with. I just write C functions, and sometime if I have a lot of related functions, say to interface with an external A/D converter for example, I will place the related functions into a 'tabbed' arduino edit window and include it in the main sketch, that way I can easily 'lift' them (copy and paste) into other sketches if I need them. Works for me. I'm kind of aware of the advantages of classes and their ability to have multiple instances but my brain is to small to figure out how and when to take advantage of such features and power. So straight C works for me and the Arduino platform allows me to swim in the same lake with all the bigger C++ guru fish.  smiley-grin
Logged

Belgium
Offline Offline
Edison Member
*
Karma: 58
Posts: 1731
Arduino rocks; but with my plugin it can fly rocking the world ;-)
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

So straight C works for me and the Arduino platform allows me to swim in the same lake with all the bigger C++ guru fish.  smiley-grin
It is a big lake. Feel welcome  smiley-grin
Best regards
Jantje
Logged

Do not PM me a question unless you are prepared to pay for consultancy.
Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 331
Posts: 16518
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So straight C works for me and the Arduino platform allows me to swim in the same lake with all the bigger C++ guru fish.  smiley-grin
It is a big lake. Feel welcome  smiley-grin
Best regards
Jantje

Thanks, I do feel I belong and I'm pretty safe here as most of the C++ fishes around these parts are not predatory, but I do have to keep a careful eye out for Coding Badly as he can be unpredictable at times.  smiley-grin
Logged

Offline Offline
God Member
*****
Karma: 21
Posts: 650
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Used properly C++ is very appropriate for embedded work.

And when used improperly C++ is very inappropriate for embedded work.

Actually, I think the OP has an important and valid point regarding the nature of C vs C++.

C is "small" langauge. Compact, purpose designed to build an OS (Unix, of course.) There was only enough added to the language to make it do what was needed, no more. It was designed to keep the programmer "close to the metal".

C++ is a "large" language. It was developed because for _very_ large projects, C lacked some features that made maintaining and re-using code in large code bases more manageable. In C++, the "metal" can seem a long way away.

But there's the problem. A lot of C++ has to be avoided, or treated very carefully, when doing embedded work. As a large language, with lots of (often redundant) features (redundant in the sense that it gives you several ways of accomplishing the same thing), if you aren't careful you can easily get code bloat with lots of stuff being wheelbarrowed in under the hood, so to speak.

So what is an embedded programmer to do? Simple: you ruthlessly "subset" C++, to just the bits you need, and everything will still be fairly efficient.

And guess what? That subset looks pretty close to original C! Funny, that.

Now, there is a bit of "syntactic sugar" you get when using C++ over C, even in the heavily subsetted form. You can use methods and classes rather than structures and functions, which are tied together a bit more closely, and perhaps more elegantly. But do you really _need_ all that additional data hiding and abstraction in an embedded project? Probably not. A well-written and structured pure C program for an embedded project will probably be just as maintainable and manageable as its well-written C++ counterpart. The code base is never going to get so large, and the programming teams maintaining it so large that the language differences really kick in to affect productivity.

And, as a final word, watch out for that string class. :-)
« Last Edit: September 17, 2012, 12:39:11 pm by pico » Logged

WiFi shields/Yun too expensive? Embeddedcoolness.com is now selling the RFXduino nRF24L01+ <-> TCP/IP Linux gateway: Simpler, more affordable, and even more powerful wireless Internet connectivity for *all* your Arduino projects! (nRF24L01+ shield and dev board kits available too.)

Dubai, UAE
Offline Offline
Edison Member
*
Karma: 21
Posts: 1670
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Pico, if my vote counted for anything I would vote for your post as the definitive statement on all things OO, C++ and Arduino.

It really annoys me that everyone jumps to the defense of Arduino as an object orientated platform when the truth is if you can't make your own judgement as to whether its OO or not, your efforts to learn OO and Arduino at the same time will lead to endless frustration.

Anyway very well put Pico.

Duane B

rcarduino.blogspot.com

Logged


Pages: [1] 2   Go Up
Jump to: