cppcoder
cppcoder

Reputation: 23135

What is the data type of vptr?

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?

Upvotes: 4

Views: 2001

Answers (3)

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361612

It has compiler-dependent type which may be anything as long as the compiler understands it. As the language doesn't say anything about vptr, neither programmers use it in their code, compilers are free to create any arbitrary type for implementing runtime polymorphism. That type doesn't has to be conformant with the C++ language.

Upvotes: 2

Peter Alexander
Peter Alexander

Reputation: 54290

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.

Upvotes: 2

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385264

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.

Upvotes: 12

Related Questions