Reference for C++ Virtual Pointer.
Questions tagged [vptr]
94 questions
8
votes
3 answers
How many vtable and vpointers will be created in this example?
Here is the program on vtables. Am I understanding is correct on vtables and v-pointers.
Class B
{
public:
virtual Void Hello()
{
cout<<"Hello Base";
}
};
class D: public B
{
public:
virtual void Hello()
{
cout<<"Hello…

dexterous
- 6,422
- 12
- 51
- 99
8
votes
2 answers
Type trait to identify primary base class
If I have a class Base, with at least one virtual function, and a class Derived which inherits singly from this then (uintptr_t)derived - (uintptr_t)static_cast (derived) is guaranteed (by the Itanium ABI) to be zero, even though Derived is…

jleahy
- 16,149
- 6
- 47
- 66
7
votes
1 answer
Dynamic_cast on non polymorphic types
I can understand why dynamic_cast does work in this case :
#include
struct A{
virtual ~A() = default;
};
struct B {
virtual ~B() = default;
};
struct C : A, B{};
void f(const A &a) {
if(auto p = dynamic_cast

Antoine Morrier
- 3,930
- 16
- 37
7
votes
3 answers
Why does my C++ object loses its VPTr
While debugging one of the program's core dump I came across the scenario where its contained object which is polymorphic loses its VPTr and I can see its pointing to NULL.
What could be the scenario when an object loses its VPTr.
Thanks in…

brijesh
- 120
- 1
- 7
7
votes
2 answers
Why is vptr stored as the first entry in the memory of a class with virtual functions?
For some compilers, if a class has virtual functions then its vptr can be accessed with the address of the first byte of its object. For instance,
class Base{
public:
virtual void f(){cout<<"f()"<

aaa
- 1,649
- 1
- 16
- 22
7
votes
4 answers
Alternative schemes for implementing vptr?
This question is not about the C++ language itself(ie not about the Standard) but about how to call a compiler to implement alternative schemes for virtual function.
The general scheme for implementing virtual functions is using a pointer to a table…

ManiP
- 713
- 2
- 8
- 19
6
votes
1 answer
Understanding of vtable in derived classes
I'm trying to undestand some low-level things with virtual table and inheritance.
When you create new class by inheriting two classes and adding new virtual functions, where exactly the vptr will be stored?
It seems to me, that compiler performs…

renzo
- 311
- 1
- 13
6
votes
1 answer
Obtain the vtable of a class without an object
I am attempting to implement a system similar to the first described here. That is, the (ab)use of vtable modification to change object behavior at runtime. This is part of my attempts to create an efficient type-generic wrapper in a C++ project I…
user3995702
6
votes
2 answers
How many vptr will a object of class(uses single/multiple inheritance) have?
How many vptrs are usually needed for a object whose clas( child ) has single inheritance with a base class which multiple inherits base1 and base2. What is the strategy for identifying how many vptrs a object has provided it has couple of single…

Passionate programmer
- 5,748
- 10
- 39
- 43
5
votes
8 answers
Dynamically changing the virtual pointer on runtime
So let's say I have two classes that inherit a base class that has a pure virtual function. Both of the classes implement their own version of that function, but don't add additional member variables so they have the same size. Now sometimes, in the…

slartibartfast
- 4,348
- 5
- 31
- 46
5
votes
1 answer
In the MSVC ABI, how do I reliably find the vtable given only a (void*)?
This question is specifically about non-portable MSVC ABI stuff.
I'm trying to write the equivalent of C++'s typeid in obviously-nonportable-yet-not-magic C++. For the Itanium ABI (as used on Linux/Mac), it's super easy:
const std::type_info&…

Quuxplusone
- 23,928
- 8
- 94
- 159
5
votes
0 answers
Virtual Mechanism in C++ and Java
I am having some confusion related to virtual mechanism in C++ and Java. The output of the following programs are different. I am not able to understand why?
In Java:
class Base
{
Base()
{
show();
}
public void show()…

Neetesh Dadwariya
- 717
- 8
- 10
5
votes
2 answers
location of virtual function table pointer in object
As I understand, the location of the virtual function pointer table in an object is compiler dependent.
Are there any pros/cons of placing this pointer at the beginning of the object vs at the end or vice-versa?

Bandicoot
- 3,811
- 7
- 35
- 39
4
votes
2 answers
why sizeof(Base) is not different of sizeof(Derived)
I think sizeof(Base) should be 12. Why is it 16?
Without the virtual function, I get 4 and 8.
class Base{
public:
int i;
virtual void Print(){cout<<"Base Print";}
};
class Derived:public Base{
public:
int n;
virtual void…

Z.Hc
- 41
- 2
4
votes
1 answer
Understanding virtual inheritance class vtables and vptr creation
The below code is of multiple inheritance where every class has one member variable, one normal function and one virtual function.
class basec
{
int x;
public:
basec()
{
x = 0;
}
void print()
{}
virtual void…

RKum
- 758
- 2
- 12
- 33