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

A basic doubt on VPTR and VTABLES

P: n/a
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

Nov 7 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Subhransu Sahoo" <sy******@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
: 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

Nov 7 '06 #2

P: n/a
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
Nov 7 '06 #3

P: n/a

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 '06 #4

P: n/a

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 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.