I'll try to post this in a clear way, despite my poor english and the poorer C (class) knowledge.
I have a number of Adafruit NeoPixel objects (they are classes, right?).
I wrote some functions that make lighting effects and now I'd need to call these functions passing as argument the name / class /object to act upon.
This is brief code example, where the MakeCross function has to be called for different objects:
Actually I do not have error from the IDE, but the function does not do anything as if it is never called.
I'm sure there is a way to do it, please tell me, thanks
void MakeCross(Adafruit_NeoPixel pix, uint32_t c) {
That attempts to make copy of the object you're passing (also known as "pass by value"). That's wasteful of resources when large objects are involved.
It's even worst for the Adafruit_NeoPixel class because it uses dynamic allocation for the pixel memory. The class has no custom Copy Constructor. The Adafruit author should have provided one or specified the Copy Constructor as deleted. Then your code would have caused a compiler error and you would have known something was wrong.
The change to your code was simple. The the implications of using a Reference are profound. You need to study them if you wish to advance farther in your C++ coding.
That is why in my classes I always declare the assignment operator and copy constructor as private. This will cause a compile error if use is attempted. My feeling is if you want to support those operations you should provide the functions rather than rely on the defaults. For example in my PW_INA219 library.
I second that. One major pitfall of the unintended use of copy is that any changes made to the copy are thrown away on return. This can cause confusion if the function called was expected to change the original.
To make things even more confusing, the reverse is true with the Adafruit_NeoPixel class since it uses dynamically-allocated memory to store the actual pixel data. When passed by value, the default constructor will make a shallow copy. So, the copy's internal pointer will point to the same memory block as the original's. That means changes made to the copy's pixel data in the function will change the original's pixel data because they are the same data.
I'm sad since sometimes I do not understand the spirit of some comments.
I never said that the posted code was made to compile and run, but only to make evident how I defined the function (MakeCross) and how I invoke it.
It is obvious that there are missing pieces not directly involved in the function call.
If I did something wrong please accept my apologies.