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:
//stuff.h
//use a shortcut macro to reduce error from copy-pasting
#define PTRGB \
public: \
quint16 setRGB(quint16 v, int i); \
quint16 getRGB(int i) const {return rgb[i]}; \
private: \
quint16 rgb[3];
class CORE
{
public:
virtual quint16 setRGB(quint16 v, int i);
virtual quint16 getRGB(int i) const {return 0;}
//other functions and variables common to all of my classes
};
class A : public CORE
{
PTRGB
private:
int extraVariable;
}
class B : public CORE
{
//doesn't have setRGB implemented!
private:
double aDifferentExtraVariable;
public:
double getSpecialB() {return aDifferentExtraVariable;}
}
class C : public CORE
{
PTRGB
private:
bool isBlue;
public:
bool getIsBlue() {return isBlue;}
}
//stuff.h
//use a shortcut macro to reduce error from copy-pasting
#define PTRGB \
public: \
quint16 setRGB(quint16 v, int i); \
quint16 getRGB(int i) const {return rgb[i]}; \
private: \
quint16 rgb[3];
class CORE
{
public:
virtual quint16 setRGB(quint16 v, int i);
virtual quint16 getRGB(int i) const {return 0;}
//other functions and variables common to all of my classes
};
class A : public CORE
{
PTRGB
private:
int extraVariable;
}
class B : public CORE
{
//doesn't have setRGB implemented!
private:
double aDifferentExtraVariable;
public:
double getSpecialB() {return aDifferentExtraVariable;}
}
class C : public CORE
{
PTRGB
private:
bool isBlue;
public:
bool getIsBlue() {return isBlue;}
}
To copy to clipboard, switch view to plain text mode
//stuff.cpp
quint16 CORE::setRGB(quint16 v, int i)
{
return 0;
}
quint16 A::setRGB(quint16 v, int i)
{
#ifdef DEBUGMODE
if(i < 0 || i > 2)
{
qWarning() << "Index to setRGB is out of range.";
return 0;
}
#endif
return rgb[i] = v;
}
quint16 C::setRGB(quint16 v, int i)
{
#ifdef DEBUGMODE
if(i < 0 || i > 2)
{
qWarning() << "Index to setRGB is out of range.";
return 0;
}
#endif
return rgb[i] = v;
}
//stuff.cpp
quint16 CORE::setRGB(quint16 v, int i)
{
return 0;
}
quint16 A::setRGB(quint16 v, int i)
{
#ifdef DEBUGMODE
if(i < 0 || i > 2)
{
qWarning() << "Index to setRGB is out of range.";
return 0;
}
#endif
return rgb[i] = v;
}
quint16 C::setRGB(quint16 v, int i)
{
#ifdef DEBUGMODE
if(i < 0 || i > 2)
{
qWarning() << "Index to setRGB is out of range.";
return 0;
}
#endif
return rgb[i] = v;
}
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?
Bookmarks