A basic doubt on VPTR and VTABLES

  • Thread starter Subhransu Sahoo
  • Start date
S

Subhransu Sahoo

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
 
I

Ivan Vecerina

: 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
 
S

Subhransu Sahoo

Sorry guys,

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

Regards,
Subhransu
 
R

raxitsheth2000

Subhransu said:
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
 
R

raxitsheth2000

Subhransu said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top