c++ - 这段代码的运行结果问题
阿神
阿神 2017-04-17 13:45:56
0
2
427
阿神
阿神

闭关修行中......

reply all(2)
Peter_Zhu

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

Peter_Zhu

rootwynn is right, we also provide a tool for c++ online compilation

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template