Hello again.
I am currently using a base class for a certain type of object, which looks like this:
#include <vector>
template <class T>
class CObject
{
public:
inline virtual ~CObject () { }
virtual std::vector<T> *Output (std::vector<int> *Input) = 0;
};
The problem, that arises from this contraption is, that, in order to have some objects working in double precision and some in single precision, the pointer type from one object to another cannot be defined. At the moment, I am using a pointer of void type and an enum PRECISION with two values: PRECISION_DOUBLE and PRECISION_SINGLE. The function calls look like this:
std::pair<void*, PRECISION> SomeObject;
switch (SomeObject.second)
{
case PRECISION_DOUBLE:
((CObject<double>*) SomeObject.second)->Output (...);
case PRECISION_SINGLE:
((CObject<float>*) SomeObject.second)->Output (...);
}
If I wrote a second enumeration, one for object type, I could do all of this without a single inheritance, but if I wanted that, I would be writing in C not in C++.
So now, I am looking for different methods to achieve this.
One idea, that came to my mind was the implementation of a converter object, which would look like this:
#include <vector>
template <class T>
class CConvert : public CObject<T>
{
private:
void *Input;
std::vector<T> m_Output;
public:
std::vector<T> *Output (std::vector<int> *Input);
};
std::vector<double> *CConvert<double>::Output (std::vector<int> *Input)
{
std::vector<float> *InputVector = ((CObject<float>*) Input)->Output (std::vector<int> *Input);
m_Output.clear ();
for (std::vector<float>::iterator i = InputVector->begin (); i != Input->end (); i++)
m_Output.push_back (*i);
return &m_Output;
}
std::vector<float> *CConvert<float>::Output (std::vector<int> *Input)
{
std::vector<double> *InputVector = ((CObject<double>*) Input)->Output (std::vector<int> *Input);
m_Output.clear ();
for (std::vector<double>::iterator i = InputVector->begin (); i != Input->end (); i++)
m_Output.push_back (*i);
return &m_Output;
}
The problem with this approach would be, that, whenever a CObject<double> needs to be connected to a CObject<float>, a CConvert<double> would have to be put in between.
Any other solutions are very appreciated.
Greetings,
JaR