Topic: dynamic_cast and different compilers


Author: "Bernard TATIN" <Bernard.Tatin@lesi.univ-orleans.fr>
Date: 1999/01/29
Raw View
I've done the following code :

class A0
{
public:
    int value;
    A0() : value(0)
    {
    }
    virtual ~A0()
    {
    }
};

class A1 : public A0
{
public:
    A1()
    {
        value = 1;
    }
    virtual ~A1()
    {
    }
};


A0 *pa0 = new A1;
A1 *pa1 = 0;
try
{
    pa1 = dynamic_cast<A1 *>pa0;
}
catch(...)
{
    printf("catch ...\n");
}
if (pa1 != 0)
    printf("good cast val = %d\n", pa1->value);
...

Microsoft VC++ 6.0 give me the following results :
catch ...
while the gnu compiler from cygnus-win32 give me :
good cast val = 1

Which is right ? Can anyone tell me ?
I have a little idea but I want to be sure.

Bernard "te fais pas de bile" TATIN.




[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: AllanW@my-dejanews.com
Date: 1999/01/29
Raw View
In article <78sflp$7dq@centre.univ-orleans.fr>,
  "Bernard TATIN" <Bernard.Tatin@lesi.univ-orleans.fr> wrote:
>
> I've done the following code :
[snip]
> Microsoft VC++ 6.0 give me the following results :
> catch ...
> while the gnu compiler from cygnus-win32 give me :
> good cast val = 1
>
> Which is right ? Can anyone tell me ?
> I have a little idea but I want to be sure.

gnu is correct. But this is not a bug in VC. You must enable RTTI.



Author: comeau@panix.com (Greg Comeau)
Date: 1999/02/01
Raw View
In article <78t78p$75t$1@nnrp1.dejanews.com> AllanW@my-dejanews.com writes:
>...this is not a bug in VC. You must enable RTTI.

If RTTI is disabled, then, well, dynamic_cast should not work.
I'd consider it a VC bug.

- Greg
--
       Comeau Computing, 91-34 120th Street, Richmond Hill, NY, 11418-3214
     Producers of Comeau C/C++ 4.2.38 -- New Release!  We now do Windows too.
    Email: comeau@comeaucomputing.com / Voice:718-945-0009 / Fax:718-441-2310
                *** WEB: http://www.comeaucomputing.com ***
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: jcoffin@taeus.com (Jerry Coffin)
Date: 1999/02/01
Raw View
In article <79345h$l65$1@panix.com>, comeau@panix.com says...
> In article <78t78p$75t$1@nnrp1.dejanews.com> AllanW@my-dejanews.com writes:
> >...this is not a bug in VC. You must enable RTTI.
>
> If RTTI is disabled, then, well, dynamic_cast should not work.
> I'd consider it a VC bug.

If you attempt to do so, VC++ gives a warning:

warning C4541: 'dynamic_cast' used on polymorphic type 'class
 X' with /GR-; unpredictable behavior may result

IIRC, it's possible get that warning treated as an error if you're in
the habit of ignoring warnings.  From a viewpoint of the standard,
there's no real difference between a warning message and an error
message -- the standard only mentions a "diagnostic", and an
implementation is apparently free to document either a warning or an
error as being a diagnostic.

In any case, the compiler has documented the fact that what you're
doing is likely to give unpredictable results.  I'm hard put to
understand how it would qualify as a compiler bug when exactly that
happens.


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]