[ Example adapted from "C++ Gotchas", tutorial notes by Tom
Cargill, page 44. ]
class B {
public:
int x;
};
class D : public B {
public:
int y;
};
void foo (B a[]) // Note argument type
{
for (int i = 0; i < 6; i++)
printf ("%d %d\n", i, a.x);
}
main ()
{
D d[6];
for (int i = 0; i< 6; i++)
d.x = i;
f(d);
return 0;
}
The output of this program (on some implementation) is shown below.
Moral of the story is that inheritance and arrays do not work
together well (there is little type safety).
0 0
1 536871025
2 1
3 7455272
4 2
5 -613566757