admin管理员组

文章数量:1323553

Do base and derived classes share a virtual table? When I applied for a job, some interviewers from famous Internet companies said yes with certainty. Although I told them that the pointer value pointing to the virtual table in the derived class object is different from the pointer value pointing to the virtual table in the base class object, so they should not share the same virtual table, the interviewer still said with certainty that they share the same virtual table. So I was a little confused and came here to find the answer.

Do base and derived classes share a virtual table? When I applied for a job, some interviewers from famous Internet companies said yes with certainty. Although I told them that the pointer value pointing to the virtual table in the derived class object is different from the pointer value pointing to the virtual table in the base class object, so they should not share the same virtual table, the interviewer still said with certainty that they share the same virtual table. So I was a little confused and came here to find the answer.

Share Improve this question asked Jan 12 at 6:22 user22481460user22481460 591 silver badge4 bronze badges 4
  • 1 vtables are an implementation detail and in general not that important. And you could have answered that. That said, the details may again depend on whether the derived class actually overloads something of the base class or not. And for that we would need to know the exact details of the question. – Pepijn Kramer Commented Jan 12 at 6:50
  • If derived class overwrite any virtual function of the base class, it of course must have different vtable, with any implementation. If not, it possible share the same table, but this even can depend from optimization – RbMm Commented Jan 12 at 6:59
  • 2 The C++ standard does not even mandate that an implementation support virtual function tables. And, among implementations (compilers) that do use virtual function tables, the answer to your question is "it depends". – Peter Commented Jan 12 at 8:28
  • For what compiler, compiler version, on what platform, with what compiler flags? It's an implementation detail. The C++ standard provides no facility to programmatically access the virtual function table, nor does the standard require that such a table exists at all. An implementation COULD share a virtual table — that's up to the implementation. – Eljay Commented Jan 12 at 14:14
Add a comment  | 

4 Answers 4

Reset to default 7

It may depend on the implementation, but at least neither gcc nor clang let the base class and derived classes share vtable. There will be one vtable per class (which is then shared among all instances of the class). It would defeat the purpose of implementing runtime polymorphism by using vtables if there wasn't one vtable per class.

Of course it's an implementation issue, but unless you disable RTTI, base and derived classes can absolutely not have identical vptrs, since those are also used for typeid and dynamic_cast, and the classes just don't behave the same.

In order to share a vtable, then, they'd have to either have separate vtable ptrs and typeinfo ptrs (an additional pointer overhead per object, which would be a horrible implementation choice), or have the vptr point to a typeinfo object which is per-class, and then have that contain the pointer to a shared vtable, which would mean another level of indirection for virtual function calls, which is another horrible implementation choice.

No sane implementation will share vtables.

the pointer value pointing to the virtual table in the derived class object is different from the pointer value pointing to the virtual table in the base class object

This is one place where English language details matter, and non-native speakers can suffer here. (In the Staging Ground, it was rather apparent that the author is non-native. Fortunately, the question has been cleaned up.) Perhaps there was some miscommunication during the interview? In particular, I am looking at "in the base class object", which is slightly not well phrased. When phrasing is slightly off, people will tend to assume a correction. However, in this case I see two possible corrections, which unfortunately lead to two rather different questions.

If you change "the" to "a" then the comparison is between the pointer in a derived class object and that in a base class object (not a sub-object). From this perspective, the question is whether or not a base class and a derived class can share a virtual function table. That is, can there be one vtable for both classes?

If you change "object" to "sub-object" then the comparison is about two pointers in a single object. Every derived class object contains a base class sub-object. The base class sub-object needs to contain a pointer to a vtable so that virtual functions can be invoked via a pointer to the base class. Similarly, the derived class object must also contain a pointer to a vtable. From this perspective, the question is whether or not these two pointers must have the same value. That is, is there one vtable for this one object?

Classes and (multiple) vtables

While the implementation of vtables is not standardized, typically each class will have its own virtual function table. The base class will have one, and the derived class will have a different one. Typically, there will be two vtables, one for each class.

Theoretically, though, this does not have to be the case. If the derived class does not override any functions, the vtables for the base and derived classes might be identical. Furthermore, if runtime type information is suppressed (which can limit the capabilities of dynamic_cast), it is plausible that the compiler could optimize these two tables into one.

Keep in mind that the theory is an optimization for a rather special case. And I do not know if it is actually implemented. So, it is reasonable to conclude that different classes have different vtables, at least in any case where it matters.

An object and its (one) vtable

Within an object, a derived class often uses the virtual table pointer that is already present in the base class sub-object. Due to clever arrangement of the table, the base class can look at the table for a derived class and "see" just the part that applies to the base class. (This is similar to how a pointer-to-base can point to a derived object and "see" just the base class sub-object.) In this scenario, the values of the vpointers are the same because there is only one vpointer.

In more complex cases, there could be more than one virtual table pointer in an object, especially in the case of multiple inheritance (meaning one class with multiple base classes, as opposed to a class whose base class has a base class). If there are multiple base classes with virtual functions, then each needs its own virtual table pointer. Furthermore, it is practically impossible for these vpointers to have the same value since each base class independently determines how its vtable is structured. Hence, the derived class vpointer cannot have the same value as every base class vpointer. (It will match one of them, though, at least in the implementations I am aware of.)

However, even though the vpointers of the base classes are different, there is still only one vtable for the object. Think of the derived class vtable as consisting of base class 1 functions, followed by base class 2 functions, etc. Each base class sub-object would have a vpointer to its portion of the derived class table. So in principle, the vpointers point to the same table, just with different offsets.

In summary, for a given object, there is only one virtual function table. The derived class and one of its base classes will agree on the address of this table. If there are other base classes, their vpointers will be offset from that of the derived class, but still point into the same table.

If you havent overridden a single method then then tables would be identical and therefore shared.

If you have t overridden anything but added more virtual methods in the derived class, then the base class could use the start of the derived classes vtable. And all this only if the vtable is used for method dispatch only.

Otherwise the vtables must be different.

本文标签: cis it a same virtual table between base class object and derived class objectStack Overflow