Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > A basic doubt on VPTR and VTABLES

Reply
Thread Tools

A basic doubt on VPTR and VTABLES

 
 
Subhransu Sahoo
Guest
Posts: n/a
 
      11-07-2006
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 ublic IX, public IY {};

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

Regards,
Subhransu

 
Reply With Quote
 
 
 
 
Ivan Vecerina
Guest
Posts: n/a
 
      11-07-2006
"Subhransu Sahoo" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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 ublic 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

 
Reply With Quote
 
 
 
 
Subhransu Sahoo
Guest
Posts: n/a
 
      11-07-2006
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 ublic IX, public IY {};
>
> void main()
> {
> IY *yobj = reinterpret_cast<IY*>(static_cast<IX*>(new CA));
> yobj->fy1();
> yobj->fy2();
> }
>
> Regards,
> Subhransu


 
Reply With Quote
 
raxitsheth2000@yahoo.co.in
Guest
Posts: n/a
 
      11-07-2006

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 ublic 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

 
Reply With Quote
 
raxitsheth2000@yahoo.co.in
Guest
Posts: n/a
 
      11-07-2006

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 ublic 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Implementing polymorphism with vtables? Jef Driesen C Programming 6 07-28-2009 10:38 AM
Vtables vishnupriya.sureshbabu@gmail.com C Programming 1 10-12-2007 02:00 PM
Explanation of Vptr and V-table pai C++ 4 10-18-2006 02:09 PM
DLL Base Address VTABLEs and COM first10@btinternet.com C++ 2 05-02-2006 04:40 PM
MSVC15 - forcing vtables into the _TEXT or _CODE segment James FitzGerald C++ 2 11-25-2004 05:58 AM



Advertisments