[SOLVED] Polymorphic classes not behaving as expected.

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 *** for the actual questions to which I need answers, but first, some explanation:
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.
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.
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.
To update the colorGenerator, a colorController class oversees the whole process:

colorController.h:

#ifndef _COLORCONTROLLER_H
#define _COLORCONTROLLER_H

#include <list>
#include "colorGenerator.h"
#include "RGBPixel.h"
#include "globals.h"
#include "Arduino.h"

unsigned long millis();

typedef std::list<colorGenerator> generatorList;

class colorController
{
    public:
	virtual bool refresh();
    protected:
	generatorList generators;
};

#endif //_COLORCONTROLLER_H

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:

bool colorController::refresh()
{
    for (generatorList::iterator it = generators.begin(); it != generators.end(); ++it)
	it->refresh();
    bool dirty = false;
    for (int i = NUM_OF_LEDS-1; i >= 0; --i)
	dirty |= RGBPixels[i].refresh();
    return dirty;
}

The CC_Cascade class (derived from colorController) sets things up like this:

CC_Cascade.h

#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

CC_Cascade.cpp

#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 < NUM_OF_LEDS; ++i)
    {
	RGBPixels[i].setParent(&RGBPixels[i-1]);
	RGBPixels[i].setDelay(500-(9*i)); //FIXME: magic number only works for 50ish pixels
    }
}

CC_Cascade::~CC_Cascade()
{
    //TODO: delete generators
}

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.
Doing a little messing about, I added the following line to colorGenerator::updateColor(): RGBPixels[0].setColor(255,127,0);
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 color 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?).

So my qustions are: (***)

  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?
  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?

Below is the code of colorSource, colorGenerator and CG_EmeraldWaters:

colorSource.h:

#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

colorSource.cpp:

#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;
}

colorGenerator.h:

#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

colorGenerator.cpp:

#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() < nextColorUpdate)
	return false;
    nextColorUpdate = millis() + (unsigned long) delay;
    this->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;
}

CG_EmeraldWaters.h:

#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

CG_EmeraldWaters.cpp:

#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);
}

From what I can see with quick glance, the code looks fine.
However declare the initial updateColor() prototype as pure virtual, this should hopefully fix the issue:

virtual void updateColor() = 0;

Also have you considered the updateColor as a pure virtual member of colorSource ( no implementation, leave that in colorGenerator ).

To save me an awful lot of copying and pasting, can you package up the project into a .zip file and attach that to a post? (expand "Additional Options")

Include the .ino file, everything needed to make it compile.

This question has been answered on stack overflow, here.

One member downvoted and left an unhelpful comment. The comment has since been removed through multiple flagging as unhelpful, but the downvote still stands. If you appreciated my posting the answer here, please upvote my question on stack overflow.

So this is basically a cross-post? I wondered why you never replied to my request for the files packaged up into one .zip file.

I wasn't sure if this was a general C++ thing or specific to coding for Arduino, so I put it on two forums. Sorry I didn't reply sooner - I've been quite busy and the question was answered without a zip on stack overflow. I have linked to the answer from here so others can benefit if they have similar problems.