C.152: Never assign a pointer to an array of derived class objects to a pointer to its base
C.152:永远不要将派生类数组的指针赋值给基类指针
Reason(原因)
Subscripting the resulting base pointer will lead to invalid object access and probably to memory corruption.
作为赋值结果的基类指针的下标运算会引起无效的对象访问并可能发生内存破坏。
Example(示例)
Enforcement(实施建议)
- Flag all combinations of array decay and base to derived conversions.
- 提示所有数组退化和基类类型向派生类类型转换的情况。
- Pass an array as a span rather than as a pointer, and don't let the array name suffer a derived-to-base conversion before getting into the span
- 使用span传递数组而不是指针,也不要再放入span之前让数组名经过一次派生类向基类类型的转换。
阅读更多更新文章,请关注微信公众号【面向对象思考】