There are 2 classes:
class A
{
private:
double a1, a2;
...
};
class B : public A
{
private:
double b1, b2;
};
and a generic container
template <typename Item>
struct TList
{
typedef std::vector <Item> Type;
};
template <typename Item>
class GList
{
private:
typename TList <Item>::Type items;
} ;
There 4 containers of objects
GList <A> A1;
GList <B> B1;
GList <A*> A2;
GList <B*> B2;
Are those conversions (up/down) allowed or not:
1] GList <B> B3 = dynamic_cast <GList <B> &> (A1);
2] GList <A> A3 = static_cast <GList <A> &> (B1);
3] GList <B*> B4 = dynamic_cast <GList <B*> &> (A2);
4] GList <A*> A4 = static_cast <GList <A*> &> (B2);
Is there any way how to convert list of objects to list of parent objects and vice versa?
Updated question
And what about reinterpret_cast?
1] GList <B> B3 = reinterpret_cast <GList <B> &> (A1);
2] GList <A> A3 = reinterpret_cast <GList <A> &> (B1);
3] GList <B*> B4 = reinterpret_cast <GList <B*> &> (A2);
4] GList <A*> A4 = reinterpret_cast <GList <A*> &> (B2);
dynamic_castworks with only polymorphic type. HereGListis not polymorphic. Hence it would not even compile!If you make this polymorphic, even then this cast wouldn't work, since there is no relationship between
GList<B>and<GList<A>. They're like two different unrelated types.In fact, they're related in the same way as
GList_BandGList_Aare related (suppose, if you define two different classes with such names). Or even better, they're related in the same way as Java and JavaScript, Car and Carpet are related.