Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why vptr is not static?

Every class which contains one or more virtual function has a Vtable associated with it. A void pointer called vptr points to that vtable. Every object of that class contains that vptr which points to the same Vtable. Then why isn't vptr static ? Instead of associating the vptr with the object, why not associate it with the class ?

enter image description here

like image 200
Harsh Maurya Avatar asked Dec 17 '12 12:12

Harsh Maurya


People also ask

Is virtual table static?

The vtable is essentially static. But you need a vptr member actually inside the object to do virtual dispatch and other RTTI operations.

What is the type of VPTR?

It has no type. It's an implementation detail unspecified by the standard; it is not part of the language.

What is the purpose of VPTR?

The compiler places the addresses of the virtual functions for that particular class in the VTABLE. In each class with virtual functions, it secretly places a pointer, called the vpointer (abbreviated as VPTR), which points to the VTABLE for that object.

Where is VPTR memory stored?

Vptr and Vtable get stored in Data Segment... Vtable is like an array of function pointer. Vtable and Vptr is creating at compile time which will get memory in run time and vtable entries are virtual function addresses .


1 Answers

The runtime class of the object is a property of the object itself. In effect, vptr represents the runtime class, and therefore can't be static. What it points to, however, can be shared by all instances of the same runtime class.

like image 89
NPE Avatar answered Oct 14 '22 00:10

NPE