E
Eric
This IS material from a CS class on object oriented programming. It is
NOT my homework.
Consider the following:
struct A {short i; void f () {cout << "A::f()\n";}};
struct B : A {long j; void f () {cout << "B::f()\n";}
void g () {cout << "B::g()\n";}};
{
A* const a = new B[10]; // dangerous (1)
a[0].f(); // A::f();
// a[1].f(); // undefined (2)
// delete [] a; // undefined (3)
}
I have questions about three lines, which are marked.
(1): Why might this be considered dangerous?
(2): This line is commented out and marked as undefined. And yet, when I
remove the comment, it compiles and runs, producing the same output as
a[0].f(), that is, A::f(). Why is this?
(3): Again, though this is marked as undefined, when I uncomment it, it
executes fine.
I haven't been able to introduce complications. Why then, might these
lined be marked as such?
Thanks,
Eric
NOT my homework.
Consider the following:
struct A {short i; void f () {cout << "A::f()\n";}};
struct B : A {long j; void f () {cout << "B::f()\n";}
void g () {cout << "B::g()\n";}};
{
A* const a = new B[10]; // dangerous (1)
a[0].f(); // A::f();
// a[1].f(); // undefined (2)
// delete [] a; // undefined (3)
}
I have questions about three lines, which are marked.
(1): Why might this be considered dangerous?
(2): This line is commented out and marked as undefined. And yet, when I
remove the comment, it compiles and runs, producing the same output as
a[0].f(), that is, A::f(). Why is this?
(3): Again, though this is marked as undefined, when I uncomment it, it
executes fine.
I haven't been able to introduce complications. Why then, might these
lined be marked as such?
Thanks,
Eric