By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,501 Members | 1,704 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,501 IT Pros & Developers. It's quick & easy.

C-style casts to subclass of polymorphic base

P: n/a
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.
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Christopher Benson-Manica <at***@nospam.cyberspace.org> 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.
Jul 22 '05 #2

P: n/a

"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in message
news:ch**********@chessie.cirr.com...
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
Jul 22 '05 #3

P: n/a
David Hilsee <da*************@yahoo.com> 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.
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.