草庐IT

c++ - 将空基类指针转换为子类指针?

我有一些代码使用了从基类类型到子类类型的有点偷偷摸摸的转换,其中子类类型被指定为模板参数。我假设因为基类没有声明数据成员并且大小为零,所以基类指针地址将与子类相同,并且转换将成功。到目前为止代码运行正确。这是我正在做的事情的简化版本:templatestructCppIterator{CppIterator(constRangeT&range){...}//...methodscallingRangeT'smembers};//Baseclass,providesbegin()/end()methods.templatestructCppIterableBase{CppIterator