Base* pb = new Derived[4]; Arrays do not provide polymorphic properties. When you obtain array elements using pb[0], pb[1],..., what you obtain is not Derived. Object, in your case, the pb pointer is offset by the address length of sizeof(int) each time. It happens that your Base class and Derived class have data member variables, so each output value is actually Base::data. The memory distribution should be 01|11|21|31. ‘|’ separates derived classes. What I said may not be clear, you can refer to this expert’s explanation. http://coolshell.cn/articles/9543.html
Base* pb = new Derived[4]; Arrays do not provide polymorphic properties. When you obtain array elements using pb[0], pb[1],..., what you obtain is not Derived. Object, in your case, the pb pointer is offset by the address length of sizeof(int) each time. It happens that your Base class and Derived class have data member variables, so each output value is actually Base::data. The memory distribution should be 01|11|21|31. ‘|’ separates derived classes. What I said may not be clear, you can refer to this expert’s explanation. http://coolshell.cn/articles/9543.html
rootwynn is right, we also provide a tool for c++ online compilation