I have two classes, point and pixel:
class point {
public:
point(int x, int y) : x(x), y(y) { };
private:
int x, y;
}
template <class T>
class pixel : public point {
public:
pixel(int x, int y, T val) : point(x, y), val(val) { };
private:
T val;
}
Now here’s my problem. I want to make a container class (let’s call it coll) that has a private vector of points or pixels. If an instance of coll contains pixels, I want it to have a method toArray(), which converts its vector of pixels to an array of T representing the contents of the vector.
I was going to do this with inheritance: ie, I could make a base class coll that contains a vector of points and a derived class that contains the extra method, but then I seem to run into problems since pixel is a class template.
Does anyone have suggestions? Could I do this somehow by making coll a class template?
Question: Do you mean for the private vector to contain both Points and Pixels at the same time, or just one or the other?
Question: If just one or the other, are you meaning to mix Pixels with different template parameters in the same private vector?
Assuming that it is just Point or Pixel in the private vector, and that the Pixels in the private vector all have the same template parameter, you could do something like this: