C ++的新手,但仍然掌握它。使用模板我有:
template<typename TransportType>
Automobile<TransportType>::Automobile(std::string make)
: Transport(make)
所以我有一些都继承了“运输”。例如,另一个是:
template<typename TransportType>
Aircraft<TransportType>::Aircraft(std::string make)
: Transport(make)
Automobile<Coach> * coach = new Automobile<Coach>("Volvo");
Aircraft<JumboJet> * jumbojet = new Aircraft<Jumbojet>("Boeing");`
我的问题是,如果我有一种传输类型,我如何将其“转换”为另一种 WITHOUT 将传输类型放入运行时。 EG:
*coach = dynamic_cast<*jumbojet typeid<transporttype>>(jumbojet);`
(我不确定是* jumbojet typeid的位置)
我看了一下memcpy,但似乎无法使它工作。 (我知道如果实际对象中有指针,可能会出现问题,但他们没有。)
memcpy(*jumbojet, *coach, sizeof(coach))
答案 0 :(得分:1)
不要试图这样做。您不需要在运行时进行强制转换。相反,请考虑使用虚函数(即运行时多态)来消除在运行时将基类强制转换为任意派生类的需要。