This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug tree-optimization/21407] [4.1 Regression] wrong code with upcast in C++


------- 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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]