C++ PATCH: throw (T *)NULL and dynamic_cast

Alfred Minarik a8601248@unet.univie.ac.at
Thu Sep 30 23:58:00 GMT 1999


Quite some impressive work,
but of course there is a [new] error
with dynamic_cast in member functions...
Modified from David Mazieres original bug report 24 Feb 1999.
--------
extern "C" void abort();

struct A {
  virtual ~A () {}
};

struct B : private A {
  B* a2b (A* objp) { return dynamic_cast<B*> (objp); }
};

int
main ()
{
  B b;
  A* aptr = (A*) &b;
  if (b.a2b (aptr)) abort();
  return 0;
}
---------
static_cast would correctly succeed here.
[As it might be related: static_cast
erroneously always succeeds even for
non direct bases (probably a well known error).
Only for illustration:
---------
struct A {};

struct B : private A {
  void f() 
  {
    B b;
    A* ap = &b;
    B* bp = static_cast<B*> (ap); //Ok
  }
};

struct C : private B {
  void f() 
  {
    C c;
    A* ap = (A*) &c;
    C* cp = static_cast<C*> (ap); //Error expected
  }
};

int
main ()
{
  B b;
  A* ap = (A*) &b;
  B* bp = static_cast<B*> (ap); //Error expected
        
  C c;
  A* ap1 = (A*) &c;
  C* cp = static_cast<C*> (ap1); //Error expected
}
---------
]

Alfred



More information about the Gcc-patches mailing list