vptr

Why vptr is not static?

こ雲淡風輕ζ 提交于 2019-11-29 03:18:08
问题 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 ? Instead of associating the vptr with the object, why not associate it with the class ? 回答1: The runtime class of the object is a property of the object itself. In effect, vptr represents the runtime class, and therefore can't be static . What it

Number of Virtual tables and Virtual Pointers in a C++ Program

删除回忆录丶 提交于 2019-11-28 07:02:41
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 run this program? Its heavily implementation dependent, but generally you'll get one vtable object per class that has any virtual functions (classes with no virtual functions or bases don't need them), and one vptr per object of a class with a vtable (pointing at the class's vtable). Things get more complex if you have multiple inheritance and virtual base classes -- which can

Invoking virtual method in constructor: difference between Java and C++

流过昼夜 提交于 2019-11-27 19:29:13
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()"); virt(); } void virt() { System.out.println("Derived::virt()"); } } public class Main { public static void main(String[] args) { new Derived(); } } This will output Base::Base() Derived::virt() Derived::Derived() Derived::virt() However, in C++ the result is different: Base::Base() Base::virt() // ← Not Derived::virt() Derived::Derived() Derived::virt() (See http://www

Virtual dispatch implementation details

我怕爱的太早我们不能终老 提交于 2019-11-27 06:25:24
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 (correct me if I am wrong, but this isn't the main question). Also, I believe I know how virtual functions work , that is, I can always tell which function will be called, I just need the implementation details. Suppose someone asked me the following: "You have base class B with virtual functions v1, v2, v3 and derived class D:B which overrides

Number of Virtual tables and Virtual Pointers in a C++ Program

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 01:41:24
问题 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 run this program? 回答1: Its heavily implementation dependent, but generally you'll get one vtable object per class that has any virtual functions (classes with no virtual functions or bases don't need them), and one vptr per object of a class with a vtable (pointing at the class

Invoking virtual method in constructor: difference between Java and C++

不问归期 提交于 2019-11-26 22:48:37
问题 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()"); virt(); } void virt() { System.out.println("Derived::virt()"); } } public class Main { public static void main(String[] args) { new Derived(); } } This will output Base::Base() Derived::virt() Derived::Derived() Derived::virt() However, in C++ the result is different:

Virtual dispatch implementation details

时光总嘲笑我的痴心妄想 提交于 2019-11-26 11:58:44
问题 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 (correct me if I am wrong, but this isn\'t the main question). Also, I believe I know how virtual functions work , that is, I can always tell which function will be called, I just need the implementation details. Suppose someone asked me the

Alternative virtual function calls implementations?

蓝咒 提交于 2019-11-26 03:52:12
问题 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 compilers implement the virtual mechanism through the virtual table and virtual pointer. This is not about implementation detail of virtual pointers and table. My questions are: Are there any compilers which implement dynamic dispatch of virtual functions