public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/33470]  New: Reassignment of a reference to a polymorphic class fails at runtime
@ 2007-09-18 10:49 photon at seznam dot cz
  2007-09-18 11:00 ` [Bug c++/33470] " pinskia at gcc dot gnu dot org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: photon at seznam dot cz @ 2007-09-18 10:49 UTC (permalink / raw)
  To: gcc-bugs

The following code compiles cleanly with 4.2.1. The expected output is "C" but
the executable outputs "B".


#include <iostream>
using namespace std;

struct A
{
        virtual ~A() { }
        virtual void f() { cout << "A"; }
};

struct B : public A
{
        virtual void f() { cout << "B"; }
};

struct C : public A
{
        virtual void f() { cout << "C"; }
};

int main(int argc, char **argv)
{
        B Binst;
        C Cinst;

        A &Aref = Binst;
        Aref = Cinst;

        Aref.f();
        return 0;
}


-- 
           Summary: Reassignment of a reference to a polymorphic class fails
                    at runtime
           Product: gcc
           Version: 4.2.1
            Status: UNCONFIRMED
          Severity: major
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: photon at seznam dot cz


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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/33470] Reassignment of a reference to a polymorphic class fails at runtime
  2007-09-18 10:49 [Bug c++/33470] New: Reassignment of a reference to a polymorphic class fails at runtime photon at seznam dot cz
@ 2007-09-18 11:00 ` pinskia at gcc dot gnu dot org
  2007-09-18 12:01 ` photon at seznam dot cz
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2007-09-18 11:00 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from pinskia at gcc dot gnu dot org  2007-09-18 11:00 -------
No, "B" is correct.


        A &Aref = Binst;
        Aref = Cinst;

is the same as:
Binst = (A)Cinst;


-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID


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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/33470] Reassignment of a reference to a polymorphic class fails at runtime
  2007-09-18 10:49 [Bug c++/33470] New: Reassignment of a reference to a polymorphic class fails at runtime photon at seznam dot cz
  2007-09-18 11:00 ` [Bug c++/33470] " pinskia at gcc dot gnu dot org
@ 2007-09-18 12:01 ` photon at seznam dot cz
  2007-09-18 15:12 ` photon at seznam dot cz
  2007-09-18 18:49 ` bangerth at dealii dot org
  3 siblings, 0 replies; 5+ messages in thread
From: photon at seznam dot cz @ 2007-09-18 12:01 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from photon at seznam dot cz  2007-09-18 12:01 -------
(In reply to comment #1)
> No, "B" is correct.
> 
> 
>         A &Aref = Binst;
>         Aref = Cinst;
> 
> is the same as:
> Binst = (A)Cinst;
> 

"Binst = (A)Cinst" would not compile (C is not derived from B).


-- 

photon at seznam dot cz changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |UNCONFIRMED
         Resolution|INVALID                     |


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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/33470] Reassignment of a reference to a polymorphic class fails at runtime
  2007-09-18 10:49 [Bug c++/33470] New: Reassignment of a reference to a polymorphic class fails at runtime photon at seznam dot cz
  2007-09-18 11:00 ` [Bug c++/33470] " pinskia at gcc dot gnu dot org
  2007-09-18 12:01 ` photon at seznam dot cz
@ 2007-09-18 15:12 ` photon at seznam dot cz
  2007-09-18 18:49 ` bangerth at dealii dot org
  3 siblings, 0 replies; 5+ messages in thread
From: photon at seznam dot cz @ 2007-09-18 15:12 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from photon at seznam dot cz  2007-09-18 15:12 -------
(In reply to comment #1)
> No, "B" is correct.
> 
> 
>         A &Aref = Binst;
>         Aref = Cinst;
> 
> is the same as:
> Binst = (A)Cinst;
> 

The compiler treats this case as Ainst = (A) Cinst.


-- 

photon at seznam dot cz changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID


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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/33470] Reassignment of a reference to a polymorphic class fails at runtime
  2007-09-18 10:49 [Bug c++/33470] New: Reassignment of a reference to a polymorphic class fails at runtime photon at seznam dot cz
                   ` (2 preceding siblings ...)
  2007-09-18 15:12 ` photon at seznam dot cz
@ 2007-09-18 18:49 ` bangerth at dealii dot org
  3 siblings, 0 replies; 5+ messages in thread
From: bangerth at dealii dot org @ 2007-09-18 18:49 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from bangerth at dealii dot org  2007-09-18 18:48 -------
To be quite clear: you can re-associate references (unlike pointers), so
when you do Ainst = Cinst, you copy the A-part of Cinst, but you still have
a 'B' whose 'A' component has just been overwritten.

W.


-- 


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


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2007-09-18 18:49 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-09-18 10:49 [Bug c++/33470] New: Reassignment of a reference to a polymorphic class fails at runtime photon at seznam dot cz
2007-09-18 11:00 ` [Bug c++/33470] " pinskia at gcc dot gnu dot org
2007-09-18 12:01 ` photon at seznam dot cz
2007-09-18 15:12 ` photon at seznam dot cz
2007-09-18 18:49 ` bangerth at dealii dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).