My test are based on the blog of http://arduinoetcetera.blogspot.be/2011/01/classes-within-classes-initialiser.html.
Because I would like to use this idea, I tried with a simple idea, after the instance is created just update a value.
But for some reason, the code compiles perfectly and there seems to be no error with the update method, but it DOES NOT UPDATE the variable.
But my output on the serial monitor show that the number (volatile int) doesn't update.
Object of class First has value - Number: 6
Object of class Second has value - Number: 13
Object of class Second has value - Double: 26
Object of class Second has value - Update: [0] - 20 // <--
- -
Object of class First has value - Number: 6
Object of class Second has value - Number: 13 // <--
Object of class Second has value - Double: 26
Object of class Second has value - Update: [1] - 70
- -
Object of class First has value - Number: 6
Object of class Second has value - Number: 13
code - MAIN : First thing(6, 13); [edited on request of @Mark Smith]
#include "First.h"
First thing(6, 13);
...
int newValue;
...
int ledPins[] = {
20, 70, 45, 68, 543, 321
}; // an array of pin numbers to which LEDs are attached
int pinCount = 6; // the number of pins (i.e. the length of the array)
bool plus=true;
int index=-1;
void loop() {
Serial.print("Object of class First has value - Number: ");
Serial.println(thing.Number());
Serial.println();
Serial.print("Object of class Second has value - Number: ");
Serial.println(thing.getSecond().Number());
Serial.print("Object of class Second has value - Double: ");
Serial.println(thing.getSecond().Double());
...
Serial.print("Object of class Second has value - Update: [");
if ( plus && index == (pinCount-1) ) {
plus=!plus;
}
if ( !plus && index == 0 ) {
plus=!plus;
}
// change + <--> -
(plus) ? index++ : index--;
newValue=ledPins[index];
Serial.print(index,DEC);
Serial.print("] - ");
Serial.println(newValue, DEC);
thing.getSecond().Update(newValue);
Serial.println("- - ");
code - FIRST
#include "First.h"
...
First::First(int firstNumber, int secondNumber) : widget(secondNumber) {
itsNumber = firstNumber;
}
int First::Number() {
return itsNumber;
}
Second First::getSecond() {
return widget;
}
code - SECOND : void Second::Update(int newNumber)
#include "Second.h"
...
volatile int itsNumber;
Second::Second(int newNumber) {
itsNumber = newNumber;
}
void Second::Update(int newNumber) {
itsNumber = newNumber;
}
int Second::Number() {
return itsNumber;
}
int Second::Double() {
return itsNumber*2;
}
For the complete code https://github.com/droopy4u/ARDUINO - https://github.com/droopy4u/ARDUINO/tree/master/ClassWithClasses -