In the following code I do a forced downcast of an A object to its
derivative B If I declare all member variables in A and only add
member functions in B, will this always work?
The test case vith Vis Studio 6.0 cl.exe works as expected.
class A
{
public:
int n;
};
class B : public A
{
public:
void f() { printf( "In B::f() n=%d\n", n); }
};
int main(int argc, char* argv[])
{
A a;
a.n=2;
B b;
b.n = 1;
b.f();
B* pb = static_cast<B*>(&a);
pb->f();
return 0;
}
// Output
//
//In B::f() n=1
//In B::f() n=2
steve<at>schlesinger<dot>com