LOL... questions galore
Ok... here we go:
From line 1 to 10 would be lines of code part of the header file ( or would they appear in the cpp file) ?
Both. I combined them for simplicity. You would split it as you have it now - definition of the class in the header and the code for the class (there is only the begin method there) in a cpp file.
Lines 12 and 13 would be lines of code part of the INO sketch
That is correct.
Line 12 instantiate an object of type class DSPI2 which is inherited from the class DSPI, named spi
Line 13 instantiate an object of type foo, using the object spi as a constructor parameter, this object is named MyFoo
Line 3 when instantiated, the class object spi will create a variable of type pointer to an object DSPI, this pointer is named _spi.
Line 3 this is not the same object spi then the one instantiated at line 12, it is distinct has it is named _spi.
No, _spi and spi are the exact same object. _spi is a pointer to the address of spi.
Line 5 list the class constructor code, using an intrinsic variable of type pointer to DSPI address, named spi.
Line 5 this DSPI address pointer is utilized give value to the private _spi pointer (here I have a question )
Line 6 is another class constructor using a variable of type pointer to DSPI content, named spi (same as line 5)
Line 5 and 6 are both constructors. Implementing the Overloading capabilities of classes in C++.
Line 5 and 6 Giving capability to the class foo to accept either a pointer to address of the object DSPI or pointer to the content of object DSPI
This is where the fun is - a * is a pointer, an & is a reference. They both perform the same job - making 2 variables share the same memory space and thus be the same variable with different names and scopes - but they do it in different ways.
Pointers are a really fun topic and not one that is easy for the novice to get their head around (it took me a number of years, but then I didn't have me for a teacher
You have a variable, say "spi". You get the address of the variable as a number using "&spi". That address can be assigned to a pointer which is created as "*_spi".
So say the object "spi" has been created and is located in memory at address 69.
You create the pointer variable _spi. Let's say that is located at address 123.
Into the variable _spi you place the number 69. This is the "pointer" aspect of it in that it "points" to where the "spi" variable is.
Because _spi is a pointer and not a real object you have to access it in a special way. In fact there are two ways of accessing it, depending on what you are doing.
In general, to access the members of an object through a pointer you just replace "." with "->", so "spi.begin()" becomes "_spi->begin()". This tells the compiler to "dereference" the pointer and act on the object that it is pointing to.
If it's not an object that is being pointed to but a normal variable, like an "int", then you need to manually dereference the pointer by adding a * to the beginning of it, such as "*intptr = 3", but you can ignore most of that for the time being.
That is all the "classic" way of dealing with pointers - the way that has been in C since the dawn of time. C++ introduced a new way - references.
These perform the exact same job but in a different way - one that can be a little tidier, though more obscured.
References are only ever created when passing a variable to a function as a parameter. You specify a reference as "&varname" in the parameter list, such as
That creates a new variable "spi" and places it at the same memory location as the object you pass to the function. This is a real object, but since it shares the same memory location it is the same object as the one you pass. And it has the advantage that you can still access it in the exact same way, so "outsidespi.begin()" stays as "insidespi.begin()" (for example) - the "." stays as a "."
So we have two constructors:
That says "Give me a pointer to a DSPI object and I will store that address in my _spi variable.
You would call that as
Then we have:
That says "Give me a DSPI object and I will create a new one at the same address with the same data. I will then get the address of that new object and store it in my _spi variable".
You call it as:
So either way the _spi pointer is assigned the address of your "spi" object from your class - either through being manually passed the pointer or through a reference to the object and then getting the address of that reference itself.
It all gets so confusing because of the repeated use for the same symbols (& and *) to mean different things in different places.
You have to just remember:
A * in a variable declaration is a pointer. A * in a variable assignment is a dereference.
A & in a function prototype is a "byref" indicator. An & in a variable assignment is a "Get me the address of this variable".
Line 7 is the intrinsic begin() function of any class in C++. It will be executed when the MyFoo object is instanciated.
No, "begin" is an Arduinoism. It's used to get around a problem known as the "Static Initialization Order Fiasco" and is manually called by the user in their sketch.
Line 8 is the intrinsic begin() function of object _spi.
Not intrinsic - see above.
I hope that is as clear as mud