C++ reinterpret cast ?
- by Ian
I would like to cast one object of the class PointsList to another object Points3DList (and vice versa) where:
template <class T>
class PointsList
{
protected:
std::vector <Point <T> *> points; //Only illustration, not possible with templaes
};
and
template <class T>
class Points3DList
{
protected:
std::vector <Point3D<T> *> points; //Only illustration, not possible with templaes
};
Between Point and Point3D there is no relationship (inheritance nor composition)...
template <class T>
class Point
{
protected:
T x;
T y;
};
template <class T>
class Point3D
{
protected:
T x;
T y;
T z;
};
What do you think about conversion
Points3DList <T> *pl3D = new Points3DList <T> ();
...
PointsList <T> *pl = reinterpret_cast < PointList <T> * > ( pl3D );
where pl3D represents pointer to Points3DList object.. Can reinterpret_cast be used in this case or it is better to create a conversion function? Data model in this case can not be changed...