Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the data type of vptr?

Tags:

c++

Any class having a virtual function would get an extra hidden pointer which would point to the most derived class.

What is the type of this vptr?

like image 956
cppcoder Avatar asked Dec 07 '22 20:12

cppcoder


2 Answers

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

Note that C++ doesn't say that there has to be a virtual table or a virtual "pointer" at all (though this is the most common implementation of RTTI in C++ toolchains).

Also, your analysis is wrong. In, say, GCC, usually each object gets a vptr that points to the relevant virtual table for that object's type: object has pointer, type has table.

like image 58
Lightness Races in Orbit Avatar answered Dec 20 '22 05:12

Lightness Races in Orbit


The standard does not guarantee the presence of the virtual table pointer, even though most implementations use it.

As a result, it has no type. It is simply an array of pointers.

like image 23
Peter Alexander Avatar answered Dec 20 '22 06:12

Peter Alexander