I couldn't really figure out how to form the question in a way that makes sense in a search engine, so if this has been asked already by someone else I apologize.

I have a set of classes, some of which have a member function, setRGB(quint16, int) for example, that does the exact same thing. I'd like to clean up my code so that setRGB is only defined once without resorting to external functions because it accesses private variables.

They all inherit from an abstract core class. For clarity's sake I don't want them to inherit from each other, so inheritance isn't really an option. I'm open to the idea of templates, but as I have never designed one before, I'm not sure when it's appropriate to use that concept.

Here's an example. A, B, and C all inherit from CORE:
Qt Code:
  1. //stuff.h
  2. //use a shortcut macro to reduce error from copy-pasting
  3. #define PTRGB \
  4. public: \
  5. quint16 setRGB(quint16 v, int i); \
  6. quint16 getRGB(int i) const {return rgb[i]}; \
  7. private: \
  8. quint16 rgb[3];
  9.  
  10. class CORE
  11. {
  12. public:
  13. virtual quint16 setRGB(quint16 v, int i);
  14. virtual quint16 getRGB(int i) const {return 0;}
  15. //other functions and variables common to all of my classes
  16. };
  17.  
  18. class A : public CORE
  19. {
  20. PTRGB
  21. private:
  22. int extraVariable;
  23. }
  24.  
  25. class B : public CORE
  26. {
  27. //doesn't have setRGB implemented!
  28. private:
  29. double aDifferentExtraVariable;
  30. public:
  31. double getSpecialB() {return aDifferentExtraVariable;}
  32. }
  33.  
  34. class C : public CORE
  35. {
  36. PTRGB
  37. private:
  38. bool isBlue;
  39. public:
  40. bool getIsBlue() {return isBlue;}
  41. }
To copy to clipboard, switch view to plain text mode 
Qt Code:
  1. //stuff.cpp
  2. quint16 CORE::setRGB(quint16 v, int i)
  3. {
  4. return 0;
  5. }
  6.  
  7. quint16 A::setRGB(quint16 v, int i)
  8. {
  9. #ifdef DEBUGMODE
  10. if(i < 0 || i > 2)
  11. {
  12. qWarning() << "Index to setRGB is out of range.";
  13. return 0;
  14. }
  15. #endif
  16. return rgb[i] = v;
  17. }
  18.  
  19. quint16 C::setRGB(quint16 v, int i)
  20. {
  21. #ifdef DEBUGMODE
  22. if(i < 0 || i > 2)
  23. {
  24. qWarning() << "Index to setRGB is out of range.";
  25. return 0;
  26. }
  27. #endif
  28. return rgb[i] = v;
  29. }
To copy to clipboard, switch view to plain text mode 
As far as I can tell, I can't do a similar macro for the definitions like I did with the declarations because of the class name in the "quint16 C::setRGB(quint16 v, int i)" lines, but I'm looking for similar functionality.

Any ideas?