Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > C-style casts to subclass of polymorphic base

Reply
Thread Tools

C-style casts to subclass of polymorphic base

 
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      09-03-2004
Does the following code exhibit undefined behavior?

#include <stdio.h>

class A
{
virtual int quux()=0;
};

class B : public A
{
int quux() {return(1);}
public:
unsigned int foo;
};

class C: public A
{
int quux() {return(0);}
public:
const char *bar;
};

void baz( const A &a )
{
((B*)&a)->foo=3; /* a is a C, not a B */
/* Prints 3, 00000000 */
printf( "%u, %p\n", ((B*)&a)->foo, (void *)dynamic_cast<const B*>(&a)) );
}

int main()
{
C b;
baz( b );
return 0;
}

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
Reply With Quote
 
 
 
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      09-03-2004
Christopher Benson-Manica <(E-Mail Removed)> spoke thus:

> printf( "%u, %p\n", ((B*)&a)->foo, (void *)dynamic_cast<const B*>(&a)) );

(void *)(dynamic_cast<const B*>(&a))

Sorry about that.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
Reply With Quote
 
 
 
 
David Hilsee
Guest
Posts: n/a
 
      09-04-2004

"Christopher Benson-Manica" <(E-Mail Removed)> wrote in message
news:ch9uda$c59$(E-Mail Removed)...
> Does the following code exhibit undefined behavior?
>
> #include <stdio.h>
>
> class A
> {
> virtual int quux()=0;
> };
>
> class B : public A
> {
> int quux() {return(1);}
> public:
> unsigned int foo;
> };
>
> class C: public A
> {
> int quux() {return(0);}
> public:
> const char *bar;
> };
>
> void baz( const A &a )
> {
> ((B*)&a)->foo=3; /* a is a C, not a B */
> /* Prints 3, 00000000 */
> printf( "%u, %p\n", ((B*)&a)->foo, (void *)dynamic_cast<const

B*>(&a)) );
> }
>
> int main()
> {
> C b;
> baz( b );
> return 0;
> }


Sutter's GOTW has a similar example that claims that it is not portable.
See http://www.gotw.ca/gotw/076.htm, #3 (The Cheat). Do you really want to
use this code, or are you posing a hypothetical question about undefined
behavior? It should be obvious to most experienced C and C++ programmers
that the code is using a dirty hack. I don't know if anyone has the
patience to track down the exact quote in the standard that states why it is
a hack or invokes undefined behavior.

--
David Hilsee


 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      09-04-2004
David Hilsee <(E-Mail Removed)> spoke thus:

> See http://www.gotw.ca/gotw/076.htm, #3 (The Cheat). Do you really want to
> use this code, or are you posing a hypothetical question about undefined
> behavior? It should be obvious to most experienced C and C++ programmers
> that the code is using a dirty hack. I don't know if anyone has the
> patience to track down the exact quote in the standard that states why it is
> a hack or invokes undefined behavior.


I definitely do not want to use the code; I asked because some code
that I am working on that's mostly maintained by my boss blindly uses
a C-style cast to cast a pointer to a base to a particular subclass,
rather than using dynamic_cast and checking for failure. I wanted a
firm basis for telling him that the code is dangerous as written.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Subclass of subclass Fab C++ 0 08-09-2012 09:54 AM
subclass a class in the namespace of the that subclass Trans Ruby 8 10-23-2008 07:24 AM
deep copy of a polymorphic object with only a base ptr? jacek.dziedzic@gmail.com C++ 3 08-20-2007 12:02 PM
String subclass method returns subclass - bug or feature? S.Volkov Ruby 2 03-12-2006 06:46 PM
subclass has a variable that is subclass of same superclass jstorta Java 3 02-20-2006 08:42 PM



Advertisments