M
mathieu
Hello,
I did read the FAQ on template(*), since I could not find an answer
to my current issue I am posting here. I have tried to summarize my
issue in the following code (**).
Basically I am trying to hide the `complexity` of template from the
user interface. If you look at the code DataSet should be the object
that my user manipulate. Unfortunately by doing so the object returned
by DataSet::Get is a FloatingPt, so without the virtual keyword, the
DataSet does not correctly hide the templated class.
Basically I am asking on design to resolve this issue, how one
usually hide template class in a non-templated class without making my
FloatingPt class virtual ? If this is detailed in the C++ FAQ please
give me keywords associated with this problem.
Thanks a bunch !
Mathieu
(*)
http://www.parashift.com/c++-faq-lite/templates.html
(**)
#include <iostream>
#include <vector>
class FloatingPt {
public:
/*virtual*/void Type() { std::cout << "FloatingPt" << std::endl; }
};
class Float : public FloatingPt {
public:
void Type() { std::cout << "Float" << std::endl; }
float f;
};
class Double : public FloatingPt {
public:
void Type() { std::cout << "Double" << std::endl; }
double d;
};
template <typename T>
class TDataSet
{
public:
TDataSet(int n = 10):Internal(n) {}
T &Get(int i) { return Internal; }
private:
std::vector<T> Internal;
};
class DataSet
{
public:
DataSet(bool t = false):isfloat(t) {}
FloatingPt &Get(int i) {
if( isfloat )
return FloatDS.Get(i);
else
return DoubleDS.Get(i);
}
private:
bool isfloat;
TDataSet<Float> FloatDS;
TDataSet<Double> DoubleDS;
};
int main(int , char *[])
{
DataSet ds;
ds.Get(0).Type();
return 0;
}
I did read the FAQ on template(*), since I could not find an answer
to my current issue I am posting here. I have tried to summarize my
issue in the following code (**).
Basically I am trying to hide the `complexity` of template from the
user interface. If you look at the code DataSet should be the object
that my user manipulate. Unfortunately by doing so the object returned
by DataSet::Get is a FloatingPt, so without the virtual keyword, the
DataSet does not correctly hide the templated class.
Basically I am asking on design to resolve this issue, how one
usually hide template class in a non-templated class without making my
FloatingPt class virtual ? If this is detailed in the C++ FAQ please
give me keywords associated with this problem.
Thanks a bunch !
Mathieu
(*)
http://www.parashift.com/c++-faq-lite/templates.html
(**)
#include <iostream>
#include <vector>
class FloatingPt {
public:
/*virtual*/void Type() { std::cout << "FloatingPt" << std::endl; }
};
class Float : public FloatingPt {
public:
void Type() { std::cout << "Float" << std::endl; }
float f;
};
class Double : public FloatingPt {
public:
void Type() { std::cout << "Double" << std::endl; }
double d;
};
template <typename T>
class TDataSet
{
public:
TDataSet(int n = 10):Internal(n) {}
T &Get(int i) { return Internal; }
private:
std::vector<T> Internal;
};
class DataSet
{
public:
DataSet(bool t = false):isfloat(t) {}
FloatingPt &Get(int i) {
if( isfloat )
return FloatDS.Get(i);
else
return DoubleDS.Get(i);
}
private:
bool isfloat;
TDataSet<Float> FloatDS;
TDataSet<Double> DoubleDS;
};
int main(int , char *[])
{
DataSet ds;
ds.Get(0).Type();
return 0;
}