C ++重解释cast? [英] C++ reinterpret cast?

查看:96
本文介绍了C ++重解释cast?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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
};

 template <class T>
class Points3DList
{
    protected:
            std::vector <Point3D<T> *> points;  //Only illustration, not possible with templaes
};

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

Between Point and Point3D there is no relationship (inheritance nor composition)...

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;
};

您对转化有何看法

Points3DList <T> *pl3D = new Points3DList <T> ();
...
PointsList <T> *pl = reinterpret_cast < PointList <T> * > ( pl3D );

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

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

推荐答案

未定义。不要这样做!

这篇关于C ++重解释cast?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆