C-style casts to subclass of polymorphic base

Discussion in 'C++' started by Christopher Benson-Manica, Sep 3, 2004.

  1. 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.
    Christopher Benson-Manica, Sep 3, 2004
    #1
    1. Advertising

  2. Christopher Benson-Manica <> 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.
    Christopher Benson-Manica, Sep 3, 2004
    #2
    1. Advertising

  3. Christopher Benson-Manica

    David Hilsee Guest

    "Christopher Benson-Manica" <> wrote in message
    news:ch9uda$c59$...
    > 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
    David Hilsee, Sep 4, 2004
    #3
  4. David Hilsee <> 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.
    Christopher Benson-Manica, Sep 4, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jstorta
    Replies:
    3
    Views:
    437
    jstorta
    Feb 20, 2006
  2. Replies:
    3
    Views:
    786
  3. S.Volkov
    Replies:
    2
    Views:
    213
    S.Volkov
    Mar 12, 2006
  4. Trans
    Replies:
    8
    Views:
    320
    Robert Klemme
    Oct 23, 2008
  5. Fab

    Subclass of subclass

    Fab, Aug 9, 2012, in forum: C++
    Replies:
    0
    Views:
    394
Loading...

Share This Page