Good afternoon.
I have a question. are these two codes the same? I mean they produce the same outcome but
code 1 uses initializer lists (like demonstrated in previous posts). Is the writing of code 2 considered initializer list ?
Is it just a matter of writing style ?
A constructor definition consists of three main parts: the signature, the initializer list, and the body.
blinker(byte ledPin) : ledPin(ledPin) { /* empty */ }
\--- signature ---/ \- init. list -/ \--- body ---/
A constructor declaration consists of just the signature.
blinker(byte ledPin);
\--- signature ---/
You can either define the constructor (or any method) inside of the class definition (it is implicitly "inline"), or you can separate the declaration from the definition.
The advantage of moving the definition out of the class is that it can be moved to a separate file, so it can be compiled separately (and in parallel). If you leave it in the class, it's in a header file, so it has to be compiled every time you include that header.
For small methods like your constructor, this is not an issue, and you'll probably want to keep it inline, so the compiler has more opportunities for optimization.
Your first example defines the constructor inside of the class, i.e. inline.
Your second example declares the constructor inside of the class, but defines it outside of the class.
Since the definition is in the same file as the declaration, there's really no advantage to using the second code.
If you look at the formal specification and the explanation here, it says:
class-or-identifier ( expression-list(optional) )
Initializes the base or member named by class-or-identifier using direct initialization or, if expression-list is empty, value-initialization
[...]
Names that appear in expression-list or brace-init-list are evaluated in scope of the constructor
This means that the identifier outside of the parentheses refers to the member variable. Everything else (between the parentheses, and inside of the constructor body) refers to the variable in the current scope, i.e. the parameter.
I have some issues i try to figure out that do concern the previous posts here so i thought not to cloud the forum with a new topic rather than revive this old one. I hope its not a bad call
I took the code from post #11 and i want to modify it a bit. To be more spesific:
The current code will blink a led every second. the class has one object called LED1 that takes the pin number as argument.
I tried your suggestion and ofcource it works.
I have such a poor programming knowledge. I thought that every class is mandatory to have a constructor default or not.
In this case i had to delete the constructor to make it work. take a look :
Its not a rule. It can be nice to have a constructor that does nothing. I like having them in my classes to at a glance I can see that "Oh yeah, I didn't do anything special here". Instead of looking around for code that isn't there. Some would like to have them, some not. Not a rule, just how you'd like to do it.
I understand though i get a little strage behaviour from the IDE. When i uncomment the default constructor then i get the following error from the IDE
C:\Users\CONSTA~1\AppData\Local\Temp\ccvZRI1v.ltrans0.ltrans.o: In function `global constructors keyed to 65535_0_sketch_apr25b.ino.cpp.o.1746':
<artificial>:(.text.startup+0x5c): undefined reference to `blinker::blinker()'
collect2.exe: error: ld returned 1 exit status
exit status 1
Error compiling for board Arduino Uno.
Even though quite a long time ago, but I like to understand the mechanism behind. Not just applying this practice.
Isn't it that the µ-controller - in particular in case it is just one and when it is about the controllers I/Os etc - the hardware (i.e. the controller) is always ready before the controller can begin with the execution of a program? So, when we talk about, e.g. an GPIO of an ATMega328P from an Uno-board ... what is preventing the ATMega328P from properly configuring its GPIO while it simultaneously can execute code?
I hope on some insights - thanks.
The hardware is configured by the software. The Arduino Core initializes some of the peripherals at the beginning of the main function. Constructors of global variables execute before main, if you try to use these peripherals before they are configured, your program often won't work correctly.
On AVR, the peripherals are quite simple, and not a whole lot of configuration is necessary, mostly timers for millis and PWM, the ADC prescaler, and TX/RX pins. (ArduinoCore-avr/wiring.c at 6ec80154cd2ca52bce443afbbed8a1ad44d049cb · arduino/ArduinoCore-avr · GitHub)
Writing to GPIO registers on AVR before main is probably not an issue.
More advanced ARM CPUs require much more configuration, such as configuring the MMU, selecting the clocks and prescalers for each peripheral, enabling these peripherals, configuring interrupts, etc.