Why does C++ behave this way?
- by eSKay
#include<stdio.h>
int b = 0;
class A { public: int a;};
class B: public A {
int c;
int d;
public:
B(){
b++;
a = b;
printf("B:%d\n",b);
}
};
int main() {
A* a = new B[10];
B* b = new B[10];
printf("\n%d", a->a);
a++;
printf("\n%d", a->a); // prints junk value
printf("\n\n%d", b->a);
b++;
printf("\n%d", b->a);
return 0;
}
The second printf prints a junk value.
It should figure that it is pointing to an object of type B and increment by the sizof(B).
Why does that not happen?