Can any one please let me know whether the virtual table and virtual pointer is per class or per object? If they are per object, why can't they be shared between the objects of same class instead of maintaining a copy per object?
-
The virtual table is shared, the pointer to the table is per object. If you're wondering why, I'm pretty sure that question has already been discussed on this site – KABoissonneault Jul 08 '15 at 18:24
-
1In typical implementations it is per class, but it is an implementation detail, so it is not required by standards. – zch Jul 08 '15 at 18:25
-
http://stackoverflow.com/questions/25463790/why-cant-the-virtual-function-table-pointer-vfptr-be-static-in-c Not 100% duplicate, but still relevant – KABoissonneault Jul 08 '15 at 18:27
-
Having virtual inheritance should complicate it. – Jul 08 '15 at 18:28
-
1@DieterLücking Having virtual inheritance still produces exactly one virtual table per class, not per object! – ikrabbe Jul 08 '15 at 18:42
3 Answers
It is implementation defined, but it is typically a static vtable per class, and each instance has a vptr to the vtable for the class. The reason each instance needs the pointer is because the compiler may not know at compile time which concrete implementation a particular variable is going to be, so a pointer to the relevant vtable has to be deterministically accessible from the reference. Hence the whole point of having a vtable :)

- 23,011
- 10
- 73
- 102
-
no it is not! The virtual table is always per class. There might be implementations, that carry a virtual table per object, but, if you modify the virtual table of an instance, you are changing the class of that instance. You cannot say that an instance belongs to a class, when you change the behavior of the instance as the behavior of the instance defines the class. – ikrabbe Dec 23 '19 at 12:02
-
1@ikrabbe This answer says _"it is typically a static vtable per class, and each instance has a vptr to the vtable for the class"_. That is true, and it also seems to be what you're claiming. In fact, you _gave an example of it_ in your own answer. So, what is the problem? – Lightness Races in Orbit Dec 23 '19 at 12:15
-
The problem is that the term virtual pointer seems to exist in the same domain as virtual tables, but the virtual pointer, which actually is a pointer to the virtual table, is an implementation detail, while virtual function tables are part of the calculus that led towards object oriented programming. The term is confusing. Actually if you count different virtual pointers in a program, you count the different classes that your program uses. – ikrabbe Mar 30 '20 at 14:50
Yes exactly. A virtual table is per class, it would make no sense otherwise. A virtual pointer is nothing. Don't use this term!
Just throw away your C++ and think how it would be implemented in C:
typedef VirtualFunctionsOfAClass AClassesVTable
struct AClass {
some_data
AClassesVTable *vptr;
};
struct VirtualFunctionsOfAClass {
int(*someFunc1)(struct AClass* _This, int a);
int(*anotherFunc1)(struct AClass* _This, int a);
};
static AClassesVTable vtableOfClassA = {func1, func2};
struct AClass* constructAClass() {
AClass* ac=malloc(sizeof(AClass));
ac->vptr = &vtableOfClassA;
return ac;
}
void aNonVirtualMemberFunctionOfClassA(AClass* _This, more arguments)
{
return; /* just a dummy */
}
And yes: With plain C you can assign function pointers to each object. But that's not the same as virtual functions in C++.
-
_"throw away your C++ and think how it would be implemented in C"_ what – Lightness Races in Orbit Jul 08 '15 at 19:08
-
-
who ever down voted this answer: You are not right! OO Makes absolutely no sense, if you define a virtual table per instance. – ikrabbe Dec 23 '19 at 12:04
-
They probably downvoted it because of the "a virtual pointer is nothing" nonsense, and the unnecessary C allegory. Thinking about implementation details, particularly invented ones, is not useful in C++. C++ is an abstraction. Think about it in abstract terms. – Lightness Races in Orbit Dec 23 '19 at 12:16
-
1The term virtual pointer is a very bad one. It is misleading and there is no concept that implements such a thing. From the comments below: __vptr does NOT mean virtual pointer but pointer to the virtual table. The pointer to the virtual table can be seen when you implement class logic with plain ansi C, as the C++ compiler hides this from the programmer. There is no C++ or any other subdialect (C#, Objective-C) that implements virtual functions other than shown in the C Code above. If you find some implementation that does not match the scheme of the code above, I will fix the answer. – ikrabbe Mar 30 '20 at 13:43
-
BTW: Read the original and great book from those who invented the C language: The C Programming Language -- Kerninghan & Richie where you can already read about object oriented concepts such as virtual functions and templates, that already existed when the object oriented C Dialects didn't existed. Read old source code of the X server and Motif to see OO concepts in productive use before C++ where born. The first implementations of Objective C where implemented as simple preprocessor macros. – ikrabbe Mar 30 '20 at 13:49
Virtual table is per class. Virtual pointer is per object. See here http://www.go4expert.com/articles/virtual-table-vptr-t16544/

- 10,807
- 14
- 66
- 117
-
The term virtual pointer makes no sense. The _vptr is a pointer into the virtual table. Of course each object needs a reference into the virtual table of the class. So each object with a virtual table needs a pointer into the virtual table of the class. But it points to the same virtual table for each object of the class. So a virtual pointer is nothing and the _vptr has surely same value for each object of a class! – ikrabbe Jul 08 '15 at 18:32
-
1@ikrabbe Is _vptr a standard term? Otherwise, "virtual table pointer" or "vtable pointer" is probably a better term – KABoissonneault Jul 08 '15 at 18:35
-
I just used it from the link. And yes: virtual pointer says nothing. Its the vtable or virtual table pointer, or even the "pointer into the virtual table". As I say: The term virtual pointer is ")(§"$/%=). Btw. See the link in my answer about Virtual Pointers! – ikrabbe Jul 08 '15 at 18:37
-
6So much pedantry on this site sometimes :) vptr is a pretty standard written term, and a lot of people use the term "Virtual Pointer" in reference to it. "Virtual pointer" makes as much sense as "virtual table" (the table isn't virtual, it's more technically known as a virtual method table or a dispatch table), and at the end of the day most programmers are smart enough to know what you're talking about from the context. – Gerald Jul 08 '15 at 18:48