C
cronusf
Why doesn't the following throw an exception. It seems like you
should not be able to downcast from A to B since A is not a B.
#include <iostream>
using namespace std;
class A {
public:
virtual void first();
};
class B: public A {
public:
void first();
void second();
};
void A::first() {
cout << "A::first()" << endl;
}
void B::first() {
cout << "B::first()" << endl;
}
void B::second() {
cout << "B::second()" << endl;
}
int main() {
A* a = new A;
B* b = (B*)a;
b->second();
return 0;
}
should not be able to downcast from A to B since A is not a B.
#include <iostream>
using namespace std;
class A {
public:
virtual void first();
};
class B: public A {
public:
void first();
void second();
};
void A::first() {
cout << "A::first()" << endl;
}
void B::first() {
cout << "B::first()" << endl;
}
void B::second() {
cout << "B::second()" << endl;
}
int main() {
A* a = new A;
B* b = (B*)a;
b->second();
return 0;
}