【发布时间】:2011-06-10 22:26:58
【问题描述】:
父类:
template <class T>
class Point
{
protected
T x;
T y;
};
派生类:
template <class T>
class Point3DTopo: public Point <T>
{
protected:
T z;
Face <T> *face; //Points to any face
};
我想将 PointsList 类的一个对象转换为另一个对象 Points3DTopoList(反之亦然),其中:
template <class T>
class PointsList
{
protected:
std::vector <Point <T> *> points; //Only illustration, not possible with templaes
};
template <class T>
class Points3DTopoList
{
protected:
std::vector <Point3DTopo <T> *> points; //Only illustration, not possible with templaes
};
允许这样的转换吗?
Points3DTopoList <T> *pl = new Points3DTopoList <T> ();
...
PointsList <T> *pl = reinterpret_cast < PointsList <T> * > ( pl3D );
还有反向转换?
PointsTopoList <T> *pl = new PointsTopoList <T> ();
...
Points3DTopoList <T> *pl3D = reinterpret_cast < Points3DTopoList <T> * > ( pl );
每个 Point3Topo 的 Face 指针会被初始化为 NULL 还是未定义?
【问题讨论】:
-
再来一次?让我重复一遍:这是一个糟糕的主意,不要这样做。您不需要
reinterpret_cast,您只需要花时间将一个容器逐点转换为另一个容器。而已。这里没有技巧,你不需要它们,只是一个简单的循环。再次,停止动态分配一切! -
除了你应该使用dynamic_cast的所有东西
标签: c++ derived-class reinterpret-cast