J
Jo
class A {
public:
char text_a[100];
A() { *text_a=0; }
~A() {}
};
//-----------------------------------------------------------------------------
class B {
public:
char text_b[100];
B() { *text_b=0; }
~B() {}
};
//-----------------------------------------------------------------------------
class C : public A, public B {
public:
char text_c[100];
C() { *text_c=0; }
~C() {}
};
//-------------------------------------------------------------------------------
void test() {
B *bp1,*bp2;
C c,*cp1,*cp2,*cp3;
void *p;
strcpy(c.text_a,"hello a");
strcpy(c.text_b,"hello b");
strcpy(c.text_c,"hello c");
cp1=&c;
p=cp1;
bp1=cp1; // ok
bp2=(B*)p; // resulting bp2 is WRONG!
cp2=(C*)p; // ok
cp3=(C*)bp2; // resulting cp3 is WRONG! Which is logical because bp2
is already wrong.
}
//-----------------------------------------------------------------------------
So the hot spot is the bp2=(B*)p;
What's wrong with that???
I can imagine someone saying "p is not pointing to a B object".
But if you think about it, conceptually, it does, imho.
So is it more a technical matter of compiling this!?
Maybe i'm stupid and/or missing something essential about C++.
If so, please give me a link to where i can study this right.
Cheers,
Jo
public:
char text_a[100];
A() { *text_a=0; }
~A() {}
};
//-----------------------------------------------------------------------------
class B {
public:
char text_b[100];
B() { *text_b=0; }
~B() {}
};
//-----------------------------------------------------------------------------
class C : public A, public B {
public:
char text_c[100];
C() { *text_c=0; }
~C() {}
};
//-------------------------------------------------------------------------------
void test() {
B *bp1,*bp2;
C c,*cp1,*cp2,*cp3;
void *p;
strcpy(c.text_a,"hello a");
strcpy(c.text_b,"hello b");
strcpy(c.text_c,"hello c");
cp1=&c;
p=cp1;
bp1=cp1; // ok
bp2=(B*)p; // resulting bp2 is WRONG!
cp2=(C*)p; // ok
cp3=(C*)bp2; // resulting cp3 is WRONG! Which is logical because bp2
is already wrong.
}
//-----------------------------------------------------------------------------
So the hot spot is the bp2=(B*)p;
What's wrong with that???
I can imagine someone saying "p is not pointing to a B object".
But if you think about it, conceptually, it does, imho.
So is it more a technical matter of compiling this!?
Maybe i'm stupid and/or missing something essential about C++.
If so, please give me a link to where i can study this right.
Cheers,
Jo