Click to See Complete Forum and Search --> : what exactly a virtual pointer is?


Ejaz
November 29th, 2002, 12:17 AM
Hi all,

Sorry for my ignorance but I really would like to know what exactly a virtual pointer is and its details or any web resource related to it.

Any help in this regard will be highly appriciated.

zdf
November 29th, 2002, 03:03 AM
Do you talk about virtual pointer or virtual pointer table ("vptr”)?

The virtual pointer table is created for each class that has at least one virtual function. This pointer is invisible (is used internally by c++). Roughly this is table is just a jump table: on the first column you have the name of the function, on the second column you have the address of the function.

class B
{
public: virtual foo() { } // vfptr[0] = address of B::foo
};

class D : public B
{
public: /*override*/ foo() { } // vfptr[0] = address of D::foo
}

Actually the tings are much more complicated.

Although virtual pointer table is invisible it is not fully protected against destructive actions. For example the table may be messed up just by doing a wrong cast.

Regards,

vinodp
November 29th, 2002, 01:02 PM
-----------------------------------------------------------------------------
Although virtual pointer table is invisible it is not fully protected against destructive actions. For example the table may be messed up just by doing a wrong cast.
-------------------------------------------------------------
I am not sure what zdf wants say here
but i dont think so by doing wrong cast the VTable is going to messed up
By doing wrong cast u may get some wrong function pointer but the table is going to remain intact.
Doing any cast is legal thing in C++.
If after doing wrong cast the VTable is changing, then its going to be major bug in C++.

Vinod

zdf
November 30th, 2002, 07:00 AM
Originally posted by vinodp
I am not sure what zdf wants say here...

You are right: I did not choose the right words.

I hope the following two examples will make the issue clear. The first example is actually a simplification of code I found in a commercial application (I know for some people is hard to believe it).

I have tested these examples on vc++. As far as I know the layout of the virtual functions table, in the case of multiple inheritance, is not standardized; that means you may get different result for other compilers.

#include <iostream>

using namespace std;

class B
{
public: void foo() { cout << "B::foo" << endl; }
};

class D1 : public B
{
public: virtual void goo() { cout << "D1::goo" << endl; }
public: void foo() { cout << "D1::foo" << endl; }
};

class D2 : public B
{
public: virtual void foo() { cout << "D2::foo" << endl; }
};

int main()
{
( &D1() ) -> foo(); // "D1::foo"
( (D2*) &D1() ) -> foo(); // "D1::goo"
return 0;
}



#include <iostream>

using namespace std;

class B1
{
public: virtual void foo1() = 0;
};

class B2
{
public: virtual void foo2() = 0;
};

class D : public B1, public B2
{
public: virtual void foo1() { cout << "D::foo1" << endl; }
public: virtual void foo2() { cout << "D::foo2" << endl; }
};

int main()
{
D d;
void* pv = (void*) &d;

B1* pb1 = (B1*) pv;
( (B1*)pv) -> foo1(); // "D::foo1"

B2* pb2 = (B2*) pv;
pb2 -> foo2(); // "D::foo1"

D* pd = (D*) pb2;
pd -> foo2(); // "D::foo1"
pd -> foo1(); // ooops!

return 0;
}


Regards,

vinodp
November 30th, 2002, 05:22 PM
ZDF says......
----------------------------------------------------------------------------
..... multiple inheritance, is not standardized; that means you may get different result for other compilers....
------------------------------------------------------------------------------
thats right ZDF but i think there is no concept called standard VTable in C++. Bcoz VTable is not define in C++ standard
I think there is lot of discussion going on about VTable
here (http://www.codeguru.com/forum/showthread.php?s=&threadid=219416)


I really get sick when i see & think about MI.:(
The VTable structure for MI is really complicated one. & If someone trying to do like what u posted then its really hard to think whts going on inside compiler.

------------------------------------------------------------
D d;
void* pv = (void*) &d;

B1* pb1 = (B1*) pv;
( (B1*)pv) -> foo1(); // "D::foo1"

B2* pb2 = (B2*) pv;
pb2 -> foo2(); // "D::foo1"
-----------------------------------------------------------------------------
For above case its very easy to understand whats going on inside & why its calling D::foo1 in both staetments. As its just simple conversion from & to void*.

But i really confuse following casting
------------------------------------------------------------------
B2* pb2 = (B2*) pv;
pb2 -> foo2(); // "D::foo1"

D* pd = (D*) pb2;
pd -> foo2(); // "D::foo1"
pd -> foo1(); // ooops!
----------------------------------------------------------------------
I couldn't get whats going on here
May be i will try to find out tomm.


Vinod