A basic doubt on VPTR and VTABLES

Discussion in 'C++' started by Subhransu Sahoo, Nov 7, 2006.

  1. Hi All,

    Can anyone tell me why the output of the following program is fx1 fx2 ?

    #include <iostream>
    using namespace std;

    class IX
    {
    public:
    virtual void fx1() {cout<<"fx1"<<endl;}
    virtual void fx2() {cout<<"fx2"<<endl;}
    };

    class IY
    {
    public:
    virtual void fy1() {cout<<"fy1"<<endl;}
    virtual void fy2() {cout<<"fy2"<<endl;}
    };

    class CA :public IX, public IY {};

    void main()
    {
    IY *yobj = reinterpret_cast<IY*>(static_cast<IX*>(new CA));
    yobj->fy1();
    yobj->fy2();
    }

    Regards,
    Subhransu
     
    Subhransu Sahoo, Nov 7, 2006
    #1
    1. Advertising

  2. "Subhransu Sahoo" <> wrote in message
    news:...
    : Hi All,
    :
    : Can anyone tell me why the output of the following program is fx1 fx2
    ?
    The program is triggering undefined behavior
    (when using the result of reinterpret_cast).
    The outcome you are observing is the accidental result of
    one implementation approach for virual functions that your
    compiler is using, and goes beyond the scope of this newsgroup.

    : #include <iostream>
    : using namespace std;
    :
    : class IX
    : {
    : public:
    : virtual void fx1() {cout<<"fx1"<<endl;}
    : virtual void fx2() {cout<<"fx2"<<endl;}
    : };
    :
    : class IY
    : {
    : public:
    : virtual void fy1() {cout<<"fy1"<<endl;}
    : virtual void fy2() {cout<<"fy2"<<endl;}
    : };
    :
    : class CA :public IX, public IY {};
    :
    : void main()
    NB: return type of main() shall be int in standard C++.
    : {
    : IY *yobj = reinterpret_cast<IY*>(static_cast<IX*>(new CA));
    : yobj->fy1();
    : yobj->fy2();
    : }

    To get the behavior you (probably) expect, use dynamic_cast:
    IY *yobj = dynamic_cast<IY*>(static_cast<IX*>(new CA));

    Only use reinterpret_cast when you really know what you are doing...

    hth -Ivan
    --
    http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
    Brainbench MVP for C++ <> http://www.brainbench.com
     
    Ivan Vecerina, Nov 7, 2006
    #2
    1. Advertising

  3. Sorry guys,

    I got the answer why is it so. It is because of the reinterpret_cast.
    Anyway, thanks a lot.

    Regards,
    Subhransu


    Subhransu Sahoo wrote:
    > Hi All,
    >
    > Can anyone tell me why the output of the following program is fx1 fx2 ?
    >
    > #include <iostream>
    > using namespace std;
    >
    > class IX
    > {
    > public:
    > virtual void fx1() {cout<<"fx1"<<endl;}
    > virtual void fx2() {cout<<"fx2"<<endl;}
    > };
    >
    > class IY
    > {
    > public:
    > virtual void fy1() {cout<<"fy1"<<endl;}
    > virtual void fy2() {cout<<"fy2"<<endl;}
    > };
    >
    > class CA :public IX, public IY {};
    >
    > void main()
    > {
    > IY *yobj = reinterpret_cast<IY*>(static_cast<IX*>(new CA));
    > yobj->fy1();
    > yobj->fy2();
    > }
    >
    > Regards,
    > Subhransu
     
    Subhransu Sahoo, Nov 7, 2006
    #3
  4. Subhransu Sahoo

    Guest

    Subhransu Sahoo wrote:
    > Hi All,
    >
    > Can anyone tell me why the output of the following program is fx1 fx2 ?
    >
    > #include <iostream>
    > using namespace std;
    >
    > class IX
    > {
    > public:
    > virtual void fx1() {cout<<"fx1"<<endl;}
    > virtual void fx2() {cout<<"fx2"<<endl;}
    > };
    >
    > class IY
    > {
    > public:
    > virtual void fy1() {cout<<"fy1"<<endl;}
    > virtual void fy2() {cout<<"fy2"<<endl;}
    > };
    >
    > class CA :public IX, public IY {};
    >
    > void main()
    > {
    > IY *yobj = reinterpret_cast<IY*>(static_cast<IX*>(new CA));
    > yobj->fy1();
    > yobj->fy2();
    > }
    >
    > Regards,
    > Subhransu



    Refer Inside COM by Dale Rogerson,

    --raxit sheth
     
    , Nov 7, 2006
    #4
  5. Subhransu Sahoo

    Guest

    Subhransu Sahoo wrote:
    > Hi All,
    >
    > Can anyone tell me why the output of the following program is fx1 fx2 ?
    >
    > #include <iostream>
    > using namespace std;
    >
    > class IX
    > {
    > public:
    > virtual void fx1() {cout<<"fx1"<<endl;}
    > virtual void fx2() {cout<<"fx2"<<endl;}
    > };
    >
    > class IY
    > {
    > public:
    > virtual void fy1() {cout<<"fy1"<<endl;}
    > virtual void fy2() {cout<<"fy2"<<endl;}
    > };
    >
    > class CA :public IX, public IY {};
    >
    > void main()
    > {
    > IY *yobj = reinterpret_cast<IY*>(static_cast<IX*>(new CA));
    > yobj->fy1();
    > yobj->fy2();
    > }
    >
    > Regards,
    > Subhransu



    Refer Inside COM by Dale Rogerson, Chap 1.

    --raxit sheth
     
    , Nov 7, 2006
    #5
    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. Replies:
    2
    Views:
    322
    Richard Herring
    May 2, 2006
  2. pai
    Replies:
    4
    Views:
    938
  3. Thomas Lenz

    vtbl and vptr

    Thomas Lenz, Dec 16, 2008, in forum: C++
    Replies:
    18
    Views:
    1,699
    Laurent Deniau
    Dec 17, 2008
  4. Peter Otten
    Replies:
    2
    Views:
    121
    Cousin Stanley
    Aug 10, 2013
  5. Terry Reedy
    Replies:
    0
    Views:
    120
    Terry Reedy
    Aug 10, 2013
Loading...

Share This Page