The first snippet here is taken from an untested program that I am writing to update some servos. (If there is an error in it, please ignore it, it is not relevant to my question).
The second snippet illustrates very roughly how that sort of code could be implemented in a Python style. The principal difference is the long-winded and error prone activeServos[n] can be replaced by as. And, for me at least, the second version is much easier to read..
I know the second snippet won't work in C++ but I'm wondering if there is something similar that can be done?
for (byte n = 0; n < numActiveServos; n++) {
if (activeServos[n].curState == 'M') {
activeServos[n].servo.attach(activeServos.servoPin);
activeServos[n].curMicros = activeServos[n].startMicros;
activeServos[n].curState = 'm';
}
if ((activeServos[n].curState != 'F') and (activeServos[n].curMicros < activeServos[n].endMicros)) {
activeServos[n].curMicros += servoStep;
activeServos[n].servo.writeMicroseconds(activeServos[n].curMicros);
}
else {
activeServos[n].servo.detach();
activeServos[n].curState = 'F';
}
}
for (byte n = 0; n < numActiveServos; n++) {
as = activeServo[n];
if (as.curState == 'M') {
as.servo.attach(as.servoPin);
as.curMicros = as.startMicros;
as.curState = 'm';
}
if ((as.curState != 'F') and (as.curMicros < as.endMicros)) {
as.curMicros += servoStep;
as.servo.writeMicroseconds(as.curMicros);
}
else {
as.servo.detach();
as.curState = 'F';
}
}
Make activeServos an array of pointers or references to the servo objects, or simply create a pointer or reference from the desired element of the current array as the first line of the for loop.
& can be either "address of" or "reference", depending on context.
char s = "hello";
char *p = &s[3]; // &s[3] is a pointer to the third character of s
char &q = s; // q is a reference to the string s
char& q = s; // identical to above
I like TypeName& variableName with & at type name because the type is a reference type. In TypeName* ptr the variable is of pointer type so I write * next to the type.
In &variable as right side or parameter assignment the & is an operator.
& and * were "address of" and "pointer to" from day one of the original K&R c language, back on the '70s. The double usage of & came with c++ in the mid/late '80s. c++ is an after-the-fact retrofit of OOP into c, which will always come with some compromises in the syntax. In this case, there are simply a limited number of operator characters to work with, and "breaking" the traditional c syntax was NOT an option. So, the new syntax had to be a super-set of the old, not a change to existing syntax.
The meaning is quite simple, if you simply look at the syntax - look at WHERE the operator is, in context: There can be no confusion.
If the & appears in a new variable or function argument definition, between athe typename (on the left), and the variable name (on the right), then it is the reference operator.
If the & appears outside a variable or function argument definition, immediately preceding a variable name, it is the address of operator.
It doesn't get much simpler than that. Might as well complain that {} are used to delimit scope when you think [] should have been used. Learn the language, and it makes perfect sense. But it's silly to the language just because you won't take the time to understand it.
RayLivingston:
If the & appears in a new variable or function argument definition, between athe typename (on the left), and the variable name (on the right), then it is the reference operator.
If the & appears outside a variable or function argument definition, immediately preceding a variable name, it is the address of operator.
Your description is very clear but in my Neanderthal brain I can see no reason why they aren't both called "address of" - after all, that's what it does.