C++重新解释演员?

0 c++ reinterpret-cast

我想将PointsList类的一个对象强制转换为另一个对象Points3DList(反之亦然),其中:

template <class T>
class PointsList
{
    protected:
            std::vector <Point <T> *> points;  //Only illustration, not possible with templaes
};
Run Code Online (Sandbox Code Playgroud)

 template <class T>
class Points3DList
{
    protected:
            std::vector <Point3D<T> *> points;  //Only illustration, not possible with templaes
};
Run Code Online (Sandbox Code Playgroud)

在Point和Point3D之间没有关系(继承或组合)......

template <class T>
class Point
{
    protected:

            T x;
            T y;

    public:
            Point( const T &x_, const T &y_ ) : x ( x_ ), y ( y_ ) {}
            inline T getX() const {return x;}
            inline T getY() const {return y;}
            inline void setX ( const T &x_ ) {x = x_;}
            inline void setY ( const T &y_ ) {y = y_;}
            ....
};

template <class T>
class Point3D
{
    protected:

            T x;
            T y;
            T z;
};
Run Code Online (Sandbox Code Playgroud)

您如何看待转换?

Points3DList <T> *pl3D = new Points3DList <T> ();
...
PointsList <T> *pl = reinterpret_cast < PointList <T> * > ( pl3D );
Run Code Online (Sandbox Code Playgroud)

其中pl3D表示指向Points3DList对象的指针..在这种情况下可以使用reinterpret_cast,还是最好创建转换函数?这种情况下的数据模型无法更改......

Oli*_*rth 6

这里的行为将完全未定义.不要这样做!