...
In this noncompliant code example, the for
loop uses array subscripting. Since arry array subscripts are computed using pointer arithmetic, this code also results in undefined behavior.
...
Instead of having an array of objects, an array of pointers solves the problem of the objects being of different sizes, as in this compliant solution:.
Code Block | ||||
---|---|---|---|---|
| ||||
#include <iostream> // ... definitions for S, T, globI, globD ... void f(const S * const *someSes, std::size_t count) { for (const S * const *end = someSes + count; someSes != end; ++someSes) { std::cout << (*someSes)->i << std::endl; } } int main() { S *test[] = {new T, new T, new T, new T, new T}; f(test, 5); for (auto v : test) { delete v; } } |
...