title: Polymorphic classes not behaving as expected
body: <p>I'm programming some arduino code, but things aren't quite going to plan.<br>
  What am I doing wrong here? I've read around and tried to educate myself about virtual functions, but perhaps I've missed something. Go to QUESTIONSHERE for the actual questions to which I need answers, but first, some explanation:</p>
  
  <p>The Classes RGBPixel and colorGenerator both derive from colorSource, which provides public functions getR(), getG() and getB() so that another pixel or color modifier can take a copy of their current colour.<br>
  Classes derived from colorGenerator implement the colour generation code so that they can generate their own colour, while RGBPixels have a colorSource *parent member, so they can obtain a color value from a colorGenerator or another RGBPixel.<br>
  In my example, I have one colorGenerator subclass (CG_EmeraldWaters, which should create me a variety of greens and blues), and then a number of RGBPixels in an array.   RGBPixels[0] should take its value from an instance of GC_EmeraldWaters, while RGBPixels[1] takes its value from RGBPixels[0], [2] from [1], [n] from [n-1]. The pixels seem to be pulling a color from their parent just fine, but either the first pixel in the chain isn't querying the colorGenerator properly, or the colorGenerator isn't updating properly.  </p>
  
  <p>To update the colorGenerator, a colorController class oversees the whole process:</p>
  
  <p>colorController.h:</p>
  
  <pre><code>#ifndef _COLORCONTROLLER_H
  #define _COLORCONTROLLER_H
  
  #include &lt;list&gt;
  #include "colorGenerator.h"
  #include "RGBPixel.h"
  #include "globals.h"
  #include "Arduino.h"
  
  unsigned long millis();
  
  typedef std::list&lt;colorGenerator&gt; generatorList;
  
  class colorController
  {
      public:
      virtual bool refresh();
      protected:
      generatorList generators;
  };
  
  #endif //_COLORCONTROLLER_H
  </code></pre>
  
  <p>As you can see, the controller has a list of colorGenerators and method to refresh them all (called from loop()), which unless overridden in the child class, does this:</p>
  
  <pre><code>bool colorController::refresh()
  {
      for (generatorList::iterator it = generators.begin(); it != generators.end(); ++it)
      it-&gt;refresh();
      bool dirty = false;
      for (int i = NUM_OF_LEDS-1; i &gt;= 0; --i)
      dirty |= RGBPixels[i].refresh();
      return dirty;
  }
  </code></pre>
  
  <p>The CC_Cascade class (derived from colorController) sets things up like this:</p>
  
  <p>CC_Cascade.h</p>
  
  <pre><code>#ifndef _CC_CASCADE_H
  #define _CC_CASCADE_H
  
  #include "colorController.h"
  
  class CC_Cascade : public colorController
  {
      public:
          CC_Cascade();
          ~CC_Cascade();
  };
  
  #endif //_CC_CASCADE_H
  </code></pre>
  
  <p>CC_Cascade.cpp</p>
  
  <pre><code>#include "CC_Cascade.h"
  #include "CG_EmeraldWaters.h"
  
  CC_Cascade::CC_Cascade()
  {
      colorGenerator * freshBubblingSpring = new CG_EmeraldWaters();
      generators.push_back(*freshBubblingSpring);
      RGBPixels[0].setParent(freshBubblingSpring);
      RGBPixels[0].setDelay(40);
      for (int i = 1; i &lt; NUM_OF_LEDS; ++i)
      {
      RGBPixels[i].setParent(&amp;RGBPixels[i-1]);
      RGBPixels[i].setDelay(500-(9*i)); //FIXME: magic number only works for 50ish pixels
      }
  }
  
  CC_Cascade::~CC_Cascade()
  {
      //TODO: delete generators
  }
  </code></pre>
  
  <p>So far so clear?
  Let me draw your attention to the colorController::refresh() function. What should happen is that every time it's called, there's one colorGenerator in the generators list (because the CC_Cascade constructor put it there), which is a CG_EmeraldWaters. When refresh() is called on this (through the iterator), it calls colorGenerator::refresh(), which in turn calls updateColor(). In the case of CG_EmeraldWaters, this is overriden, so CG_EmeraldWaters::updateColor SHOULD be called, giving a turquoise colour. Using some serial write statements to debug, I can see that IN FACT colorGenerator::updateColor() is called, so in that case I'd expect an orangey colour, BUT neither of these is affecting the colour of the pixels, which are all staying a purple colour as set in the CG_EmeraldWaters contructor.<br>
  Doing a little messing about, I added the following line to colorGenerator::updateColor(): <code>RGBPixels[0].setColor(255,127,0);</code>
  Rather than the orange colour I was hoping for, the first pixel alternated quickly between purple and orange, suggesting (IMHO) that my new line of code was doing its job, but then the pixel was pulling its original purple colour again from the colorGenerator, and that somehow colorGenerator::updateColor() doesn't change the colour of the colorGenerator (given that I don't get a compile error, what IS it changing?).</p>
  
  <p><strong>So my qustions are: (QUESTIONSHERE)<br>
  1) How can I change the value of colorSource::currentR(/G/B) from within colorGenerator::updateColor(), given that currentR(/G/B) is declared as protected in colorSource and that colorGenerator is directly derived from colorSource?<br>
  2) Given an instance of CG_EmeraldWaters, how can I call CG_EmeraldWaters::updateColor() via colorGenerator::refresh(), which CG_EmeraldWaters inherits, given that updateColor() is declared as virtual in colorGenerator and overriden in CG_EmeraldWaters?</strong></p>
  
  <p>Below is the code of colorGenerator and CG_EmeraldWaters:</p>
  
  <p>colorSource.h:</p>
  
  <pre><code>#ifndef _COLORSOURCE_H
  #define _COLORSOURCE_H
  
  #include "Arduino.h"
  #ifdef DEBUG
  #include "colorGenerator.h" //FIXME: delete Me
  #endif
  
  //#define byte unsigned char
  typedef byte colorStorage_t;
  
  class colorSource
  {
      public:
      colorSource();
          colorSource(colorStorage_t initialR, colorStorage_t initialG, colorStorage_t initialB);
  
      void setColor(colorStorage_t newR, colorStorage_t newG, colorStorage_t newB);
      //TODO: better implementation than this
      colorStorage_t getR();
      colorStorage_t getG();
      colorStorage_t getB();
  
      bool hasChanged();
  
      protected:
      colorStorage_t currentR;
      colorStorage_t currentG;
      colorStorage_t currentB;
  
      bool dirty;
  #ifdef DEBUG
      friend colorGenerator; //FIXME: delete Me
  #endif
  };
  
  #endif //_COLORSOURCE_H
  </code></pre>
  
  <p>colorSource.cpp:</p>
  
  <pre><code>#include "colorSource.h"
  
  colorSource::colorSource()
  {
      //nothing here
  }
  
  colorSource::colorSource(colorStorage_t initialR, colorStorage_t initialG, colorStorage_t initialB)
      :
      currentR(initialR),
      currentG(initialG),
      currentB(initialB)
  {
      //intialised in the list
      Serial.println("Constructed Color Source with initial color");
  }
  
  void colorSource::setColor(colorStorage_t newR, colorStorage_t newG, colorStorage_t newB)
  {
      currentR = newR;
      currentG = newG;
      currentB = newB;
  }
  
  colorStorage_t colorSource::getR()
  {
      return currentR;
  }
  
  colorStorage_t colorSource::getG()
  {
      return currentG;
  }
  
  colorStorage_t colorSource::getB()
  {
      return currentB;
  }
  
  bool colorSource::hasChanged()
  {
      return !dirty;
  }
  </code></pre>
  
  <p>colorGenerator.h:</p>
  
  <pre><code>#ifndef _COLORGENERATOR_H
  #define _COLORGENERATOR_H
  
  #include "colorSource.h"
  #ifdef DEBUG
  #include "RGBPixel.h" //delete me, used for debugging!
  #include "globals.h" //and me!
  #endif
  
  extern "C" unsigned long millis();
  
  class colorGenerator : public colorSource
  {
      public:
          colorGenerator(colorStorage_t initialR, colorStorage_t initialG, colorStorage_t initialB);
      bool refresh();
  
      protected:
      virtual void updateColor();
  
      unsigned long nextColorUpdate = 0;
      unsigned short delay = 40;
  };
  
  #endif //_COLORGENERATOR_H
  </code></pre>
  
  <p>colorGenerator.cpp:</p>
  
  <pre><code>#include "Arduino.h"
  
  #include "colorGenerator.h"
  
  colorGenerator::colorGenerator(colorStorage_t initialR, colorStorage_t initialG, colorStorage_t initialB)
      :
      colorSource(initialR,initialG,initialB)
  {
      //intialised in the list
      //Serial.println("Constructed Color Generator");
  }
  
  bool colorGenerator::refresh()
  {
  #ifdef DEBUG
      Serial.print("colorGenerator::refresh()");
  #endif
      if (millis() &lt; nextColorUpdate)
      return false;
      nextColorUpdate = millis() + (unsigned long) delay;
      this-&gt;updateColor();
      return true;
  }
  
  void colorGenerator::updateColor() //this function gets called (even if it has been overridden in a child class), but the code in it doesn't have the desired effect
  {
  #ifdef DEBUG
      //Serial.print("colorGenerator::updateColor()");
      //RGBPixels[0].setColor(255,127,0);
  #endif
      currentR = random(127,255);
      currentG = random(0,127);
      currentB = 0;
  }
  </code></pre>
  
  <p>CG_EmeraldWaters.h:</p>
  
  <pre><code>#ifndef _CG_EMERALDWATERS_H
  #define _CG_EMERALDWATERS_H
  
  #include "colorGenerator.h"
  #include "globals.h"
  #include "RGBPixel.h"
  
  class CG_EmeraldWaters : public colorGenerator
  {
      public:
          CG_EmeraldWaters();
  
      protected:
          void updateColor();
  };
  
  #endif //_CG_EMERALDWATERS_H
  </code></pre>
  
  <p>CG_EmeraldWaters.cpp:</p>
  
  <pre><code>#include "Arduino.h"
  
  #include "CG_EmeraldWaters.h"
  
  CG_EmeraldWaters::CG_EmeraldWaters()
      :
      colorGenerator(255,0,255) //this color seems to stick! Changes made by updateColor() aren't propogated to the pixels.
  {
      //initialised in list
      //Serial.println("Constructed Emerald Waters");
  }
  
  long random(long,long);
  
  void CG_EmeraldWaters::updateColor() //this never seems to be called!
  {
      currentR = 0;
      currentG = random(0,255);
      currentB = random(0,255);
  }
  </code></pre>
  
  <p>And finally, the main sketch file:</p>
  
  <pre><code>#include "FastSPI_LED2.h"
  #include &lt;StandardCplusplus.h&gt;
  
  #include "colorController.h"
  #include "RGBPixel.h"
  #include "globals.h"
  #include "CC_Cascade.h"
  
  colorController * currentColorController;
  RGBPixel RGBPixels[NUM_OF_LEDS];
  struct CRGB ledString[NUM_OF_LEDS];
  
  void setup()
  {
  #ifdef DEBUG
      //debugging:
      Serial.begin(9600);
      Serial.println("In Setup");
  #endif
  
    // sanity check delay - allows reprogramming if accidently blowing power w/leds
      //delay(2000);
      LEDS.setBrightness(8);
      LEDS.addLeds&lt;WS2801&gt;(ledString, NUM_OF_LEDS);
  
      currentColorController = new CC_Cascade();
  }
  
  void writeValuesToString()
  {
      for (int i = 0; i &lt; NUM_OF_LEDS; ++i)
      ledString[i] = CRGB(RGBPixels[i].getR(),RGBPixels[i].getG(),RGBPixels[i].getB());
      LEDS.show();
  }
  
  void loop()
  {
      static bool dirty = false; //indicates whether pixel values have changed since last hardware write
      //unsigned long lastHardwareWrite = 0; //time of last hardware write - only do this once per milisecond to avoid flicker (this method doesn't work, still flickers)
  
      dirty |= currentColorController-&gt;refresh();
      if (dirty)
      {
      dirty = false;
      writeValuesToString();
          delay(1); //to prevent flicker
      }
  }
  </code></pre>