------- Additional Comments From giovannibajo at libero dot it  2005-05-10 
15:48 -------
In my example, B is a (sort-of) derived class and A is a (sort-of) base class. 
The C++ frontend should use a subobject at offset 0 to represent the base 
class. When you downcast through dynamic_cast, you are not doing anything 
different from what foo2() is doing, when called with &sb.sa (read: base class).

So what am I missing? What's the difference between:

----------------------------------
/* C code */
struct A { int a; };
struct B { struct A sa; int b; }

void foo(struct A* pa)
{
  ((struct B*)pa)->b = 0;
}

void bar(void)
{
  struct B sb;
  foo(&sb.sa);
}
----------------------------------

and

----------------------------------
/* C++ code */
struct A { int a; };
struct B : struct A { int b; }

void foo(struct A* pa)
{
  static_cast<B*>(pa)->b = 0;
}

void bar(void)
{
  struct B sb;
  foo(&sb);
}
----------------------------------

?

-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=21407

Reply via email to