As can be seen, D3
introduces a new virtual function, @function3()
, in the middle of the chain of inheritance. I am wondering what is happening with *--vptr and vtable when this happens. D3 is a 'kind of' a new base class now,
class Base {
public:
virtual void function1() { std::cout << "Base func1()\n"; };
virtual void function2() { std::cout << "Base func2()\n"; };
};
class D1 : public Base {
public:
virtual void function2() { std::cout << "D1 func2()\n"; };
};
class D2 : public D1 {
public:
virtual void function1() { std::cout << "D2 func1()\n"; };
};
class D3 : public D2 {
public:
virtual void function2() { std::cout << "D3 func2()\n"; };
virtual void function3() { std::cout << "D3 func3()\n"; };
};
class D4 : public D3 {
public:
virtual void function1() { std::cout << "D4 func1()\n"; };
virtual void function3() { std::cout << "D4 func3()\n"; };
};
int main() {
D3 d3;
}
but when I see vtable entries, what I can see are function1()
, function2()
.
I thought entries have to be function2()
, function3()
.
Why can't I get what I thought?
Base
only has two entries. There will be an additional entry forfunction3
that is not shown (or might be below the bottom of the window; check the scroll bar). – 1201ProgramAlarmvirtual
it's virtual in all derived classes (But also seefinal
). What this means for you is you don't have to keep repeatingvirtual
. – user4581301