Why do we need virtual table?

前端 未结 5 1989
执念已碎
执念已碎 2020-12-03 11:42

I was looking for some information about virtual table, but can\'t find anything that is easy to understand.
Can somebody give me good examples(not from Wiki, please) wi

5条回答
  •  不思量自难忘°
    2020-12-03 12:06

    To answer your headline question - you don't, and the C++ Standard does not specify that you must be provided with one. What you do want is to be able to say:

    struct A {
      virtual ~A() {}
      virtual void f() {}
    };
    
    struct B : public A {
      void f() {}
    };
    
    A * p = new B;
    p->f();
    

    and have B::f called and not A::f. A virtual function table is one way of implementing this, but is frankly not of interest to the average C++ programmer - I only ever think about it when answering questions like this.

提交回复
热议问题