Reference for C++ Virtual Pointer.
Questions tagged [vptr]
94 questions
49
votes
11 answers
Alternative virtual function calls implementations?
C++ supports dynamic binding through virtual mechanism. But as I understand the virtual mechanism is an implementation detail of the compiler and the standard just specifies the behaviors of what should happen under specific scenarios. Most…

Alok Save
- 202,538
- 53
- 430
- 533
43
votes
3 answers
Virtual dispatch implementation details
First of all, I want to make myself clear that I do understand that there is no notion of vtables and vptrs in the C++ standard. However I think that virtually all implementations implement the virtual dispatch mechanism in pretty much the same way…

Armen Tsirunyan
- 130,161
- 59
- 324
- 434
20
votes
5 answers
understanding vptr in multiple inheritance?
I am trying to make sense of the statement in book effective c++. Following is the inheritance diagram for multiple inheritance.
Now the book says separate memory in each class is required for vptr. Also it makes following statement
An oddity in…

Xinus
- 29,617
- 32
- 119
- 165
19
votes
7 answers
Invoking virtual method in constructor: difference between Java and C++
In Java:
class Base {
public Base() { System.out.println("Base::Base()"); virt(); }
void virt() { System.out.println("Base::virt()"); }
}
class Derived extends Base {
public Derived() { System.out.println("Derived::Derived()");…

Xiao Jia
- 4,169
- 2
- 29
- 47
17
votes
1 answer
C++/compilation : is it possible to set the size of the vptr (global vtable + 2 bytes index)
I posted recently a question about the memory overhead due to virtuality in C++. The answers allow me to understand how vtable and vptr works.
My problem is the following : I work on supercomputers, I have billions of some objects and consequently I…

Vincent
- 57,703
- 61
- 205
- 388
16
votes
8 answers
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 ?…

Samarsh
- 565
- 5
- 18
13
votes
6 answers
Number of Virtual tables and Virtual Pointers in a C++ Program
Let say we have below program:
class A
{ public:
virtual fun(){};
};
class B:public A
{ public:
virtual fun(){};
};
int main()
{
A a1;
B b1;
}
My question is how many vtables and how many vptrs will be created, when we…

CodeCodeCode
- 459
- 1
- 12
- 21
11
votes
3 answers
Why does virtual inheritance need a vtable even if no virtual functions are involved?
I read this question: C++ Virtual class inheritance object size issue, and was wondering why virtual inheritance results in an additional vtable pointer in the class.
I found an article here: https://en.wikipedia.org/wiki/Virtual_inheritance
which…

Klaus
- 24,205
- 7
- 58
- 113
10
votes
2 answers
Does the vptr change during destruction?
I was looking at this article, and it says "Upon entry to the base class destructor, the object becomes a base class object, and all parts of C++—virtual functions, dynamic_casts, etc.—treat it that way." Does this mean that the vptr has changed…

pythonic metaphor
- 10,296
- 18
- 68
- 110
10
votes
3 answers
Virtual tables and virtual pointers for multiple virtual inheritance and type casting
I am little confused about vptr and representation of objects in the memory, and hope you can help me understand the matter better.
Consider B inherits from A and both define virtual functions f(). From what I learned the representation of an…

Artium
- 5,147
- 8
- 39
- 60
9
votes
5 answers
When exactly does the virtual table pointer (in C++) gets set for an object?
I know that for any class that has a virtual function or a class that is derived from a class that has a virtual function, the compiler does two things. First, it creates a virtual table for that class and secondly, it puts a virtual pointer (vptr)…

Abhineet Mishra
- 113
- 1
- 6
9
votes
3 answers
When does the vptr (pointing to vtable) get initialized for a polymorphic class?
This is not about "When VTABLE is created?". Rather, when the VPTR should be initialized? Is it at the beginning/end of the constructor or before/after the constructor?
A::A () : i(0), j(0) -->> here ?
{
-->> here ?
//...
-->> here ?
}

iammilind
- 68,093
- 33
- 169
- 336
8
votes
4 answers
Why is a vptr required when the derived class doesn't override the virtual function?
class base {
public:
void virtual fn(int i) {
cout << "base" << endl;
}
};
class der : public base{
public:
void fn(char i) {
cout << "der" << endl;
}
};
int main() {
base* p = new der;
char i = 5;
…

user966379
- 2,823
- 3
- 24
- 30
8
votes
5 answers
virtual table and _vptr storage scheme
Can someone explains how this virtual table for the different class is stored in memory? When we call a function using pointer how do they make a call to function using address location? Can we get these virtual table memory allocation size using a…

Vineet Jain
- 1,515
- 4
- 21
- 31
8
votes
1 answer
vtable: Underlying algorithm
My understanding of vtables is that, if I have a class Cat with a virtual function speak() with subclasses Lion and HouseCat, there is a vtable which maps speak() to the correct implementation for each Subclass. So a call
cat.speak()
Compiles…

Alex
- 871
- 7
- 23