Re: calling virtual function from base class

Discussion in 'C++' started by Phui Hock, Sep 8, 2004.

  1. Phui  Hock

    Phui Hock Guest

    I've found something funny :)

    #include <iostream>
    using namespace std;

    class Base
    {
    public:

    void EatPoo()
    {
    cout << "Base::EatPoo" << endl;
    }
    };

    class NotBaseDerived
    {
    public:
    void EatPoo()
    {
    cout << "NotBaseDerived::EatPoo" << endl;
    }
    };

    int main()
    {
    NotBaseDerived nbd;

    Base* r = reinterpret_cast<Base*>(&nbd);

    r->EatPoo();
    return 0;
    }

    The output is Base::EatPoo(). I know this is gonna break the program if
    Im builing one like this but the idea is compiler already did early
    binding for r before the program is actually run :)
     
    Phui Hock, Sep 8, 2004
    #1
    1. Advertising

  2. Phui  Hock

    JKop Guest

    Phui Hock posted:

    > I've found something funny :)
    >
    > #include <iostream>
    > using namespace std;
    >
    > class Base
    > {
    > public:
    >
    > void EatPoo()
    > {
    > cout << "Base::EatPoo" << endl;
    > }
    > };
    >
    > class NotBaseDerived
    > {
    > public:
    > void EatPoo()
    > {
    > cout << "NotBaseDerived::EatPoo" << endl;
    > }
    > };
    >
    > int main()
    > {
    > NotBaseDerived nbd;
    >
    > Base* r = reinterpret_cast<Base*>(&nbd);
    >
    > r->EatPoo();
    > return 0;
    > }
    >
    > The output is Base::EatPoo(). I know this is gonna break

    the program if
    > Im builing one like this but the idea is compiler already

    did early
    > binding for r before the program is actually run :)
    >
    >


    Exactly, and why? Because you working with it in the
    context of a "Base".

    One thing though, even though neither of your classes are
    polymorphic (by which I mean that they have no virtual
    functions), and even though you have no member variables,
    still I don't think you can be certain that your code is
    "defined behaviour". While it may sound absurd, according
    to the Standard, your compiler MAY store objects in memory
    in whatever fashion tickles its fancy. As such, even though
    there's no member variables, the compiler has the choice of
    storing some data wherever it wants. One of these weird
    methods chosen by the compiler may result in a scenario in
    which your code may not work as expected.

    Anyway, long story short, your code exhibits undefined
    behaviour (although I leave myself to be corrected if by
    any chance there's some subtlety I've missed)


    -JKop
     
    JKop, Sep 8, 2004
    #2
    1. Advertising

  3. Phui  Hock

    Jerry Coffin Guest

    "Phui Hock" <> wrote in message news:<chmb0n$>...

    [ ... ]

    > NotBaseDerived nbd;
    >
    > Base* r = reinterpret_cast<Base*>(&nbd);
    >
    > r->EatPoo();


    What you have here is simple undefined behavor, so the compiler is
    free to do anything it feels like. Consider it pure (bad) luck that
    you got any output at all instead of the program simply crashing.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
     
    Jerry Coffin, Sep 8, 2004
    #3
    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. tomek
    Replies:
    2
    Views:
    697
    Andrey Tarasevich
    Dec 1, 2003
  2. tomek
    Replies:
    3
    Views:
    774
    David Fisher
    Nov 30, 2003
  3. Phui  Hock
    Replies:
    4
    Views:
    472
  4. Andreas Lagemann
    Replies:
    8
    Views:
    514
    Mike Wahler
    Jan 10, 2005
  5. tiwy
    Replies:
    0
    Views:
    457
Loading...

Share This Page