Possible Duplicate:
Can a pointer to base point to an array of derived objects?
I am practicing what learned and I tried the folowing:
#include <iostream>
struct S {
S() : val(0) {}
int val;
};
struct D : S {
D() : val(1) {}
int val;
};
void f(S *s) {
for (int i = 0; i < 5; i++, s++)
std::cout << s->val;
}
int main() {
D d[5];
f(d);
}
What I find weird is that the output is 01010
instead of 11111
like I expected. So it seems to be getting the val
member from the S
class instead of the D
class on every other loop. But why?