C++ reinterpret cast ?

Posted by Ian on Stack Overflow See other posts from Stack Overflow or by Ian
Published on 2011-01-16T19:44:29Z Indexed on 2011/01/16 19:53 UTC
Read the original article Hit count: 172

Filed under:
|

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...

© Stack Overflow or respective owner

Related posts about c++

Related posts about reinterpret-cast