new to both managed c++ and stack overflow so I hope I'm doing this right.
I'm having a hard time finding a way to write a function with a base class parameter that requires a derived class's class variable.
My base class is called ImageBase and has two class variables: int rows, cols;
public ref class ImageBase
{
public:
int num_rows;
int num_cols;
}
Derived from that is an abstract, templated class called ImageT that only contains a templated 2-D array, pix:
template<class T> public ref class ImageT abstract : public ImageBase
{
public:
property T** pix
{
public: T** get() { return rows; }
protected: void set(T** val) { rows = val; }
}
}
Derived from ImageT are RealImage, ShortImage and ComplexImage, which simply change the template type "T" to float, short or complex respectively. Here is a little MS Paint diagram of the class structure if this is easier to understand:
My question is: if I were to write a very general function like:
void printPixels(ImageBase^ img)
so that I could pass it a RealImage, ComplexImage etc, how would I access the variable "pix" from an ImageBase type?
You cannot. At least not without tricks.
To solve this, I would recommend to templatize the printPixels()
function:
template<class T> void printPixels(ImageT<T>^ img)
If you really need it, you could add a printPixels(ImageBase^ img)
that checks the type of img, casts it, and calls the right printPixels(ImageT<T>^ img)
for the real work.