0

I am python experienced, an arduino noob, and absolutely no experience of C other than what has been forced upon me by arduino use.

I have just spent a day hammering google for the precise syntax of creating and using a class for user libraries, and have finally won.

However, I find that there are two forms that both seem to work for instantiating an object in the main program.

With my python experience, I find it easier to recognise the obj= style, used by Alan Zucconi in his examples, though the arduino.cc library tutorial on the Morse library uses the other form.

Is either preferrable, or are they equivalent in every way? Are there any subtle side effects of which I am unaware, that will bite me if the interfaces get more complicated, or some assumptions get corrected in future releases of the arduino IDE or compiler?

#include "Minimal_library.h"

// allocate buffer to be used
#define BUF_SIZE 40
char buff[BUF_SIZE+1];  

// which of these two instantiation calls works?

// Minimal_library obj = Minimal_library(BUF_SIZE, buff);  //alanzucconi - works
Minimal_library obj(BUF_SIZE, buff);   //  Morse - that seems to work as well now
...

I presume that any private variables defined within the class become fully fledged persistent variables unique to the object created (it's been a long and frustrating day, not quite got around to testing the persistence or uniqueness yet, compiling without error, and then working, are enough for today!)

  • Should be equivalent. See en.wikipedia.org/wiki/Copy_elision – Edgar Bonet Nov 18 '16 at 19:05
  • I'm sure that wikipedia article was relevant to those in the know, however I don't understand a word of it. Do you have anything that simplifies rather than complicates? – Neil_UK Nov 18 '16 at 19:18
  • Put out of your head the idea that the Arduino IDE has some special syntax. It's just C++ which is very widely covered on the Web in tutorials. See my post about How the IDE organizes things - there is some pre-processing of sketches before they go to the C++ compiler, but apart from that, whatever works in C++ works on the Arduino (exceptions excepted because they are disallowed by a compile-time option). – Nick Gammon Nov 19 '16 at 6:48
2

The statement

Minimal_library obj(BUF_SIZE, buff);

means “create an object named obj, initialized by calling the class constructor with the supplied parameters”.

On the other hand,

Minimal_library obj = Minimal_library(BUF_SIZE, buff);

means “create an unnamed object initialized as before, then copy it into an object named obj, then delete the unnamed object”.

The compiler would normally optimize the second form into the first one. It is even allowed to do so in some weird cases where they may not be equivalent. In the end, it's mostly a matter of style. I prefer to do the optimization myself and write the first form. But if you find the second form more readable, go ahead: you can normally trust the compiler to optimize out the unneeded copy.

| improve this answer | |
  • Thanks, now I understand the copy_elision refernce. I guess it's just the syntax that seems less obvious than python, but partial knowledge is not always helpful. I might understand obj=MinLib(params) to be as you describe, with the leading MinLib taking the role of a cast, but that doesn't work for the first form. Could you link me to the simplest documentation that describes how storage in a class instance is organised. I expect header/public to be persistent attributes, and local on the stack, are header/private persistent by the instance? Can the class take variables like python? – Neil_UK Nov 19 '16 at 4:33
  • @Neil_UK: I do not have such a reference, as I learned from a combination of books and online resources. public/private does not make a difference storage-wise, the whole object is stored as a single entity in static RAM (for a global or static local), on the heap (if you new it) or on the stack (for a non-static local). A member qualified as static in the class definition belongs to the class itself, not to any particular instance, and is stored in static RAM. – Edgar Bonet Nov 19 '16 at 10:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.