Foward decleration casting craziness
Hi there...
I just found a bug in a very well known compiler and I was wondering if
others have found the same problem. Consider the code snippet
class C; // Foward decleration
class A {
public:
// Here we cast to C but we don't know how to cast an A to
C* getAasC{ return (C*)this; };C
int a;
};
Note that this code will compile (in my compiler at least) even though
it does not know how to cast an A to a C. In another file say we define
C:
class B {
public:
int b;
};
class C : public B, A {
public;
int c;
}
In the first code snippet, the compiler does not know how to cast an A
to a C. Therefore it does a reinterpret cast instead of a static cast.
Consider the following code:
A * a = new C();
a->a = 50;
printf("A: %d, C: %d", a->a, a->getAasC()->c)
Depending on how you compiler does the reintpret cast the above will
either segfault or print:
A: 50, C: 50
My compiler didn't even give me a warning! I think it should give you
an error if you try to cast something and it doesn't know how to do the
cast.
Has anyone else run into something like this?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]