Compiler added optimization causes different behavior for “final” methods [closed]

守給你的承諾、 提交于 2019-12-13 16:14:29

问题


I had asked a post with the same title. But due to misinterpretation, all the answers were discussing if it's a valid code/behavior or not.

However the main question is about, if the optimization will kick-in or not ?

With reference to the linked post, see the genuine case:

D *pD = new D;
pD->foo();  // `D::foo()` is `final`

Will all the compilers optimize pD->foo() call by not kicking-in virtual mechanism ?

[Note:
Why I gave the example with bad code in earlier question?
I am aware that, casting from base to child in above way is not a good code. Main intention was to check if the Java-like optimization for final happens or not and the first way was the only way one can verify it without going down to assembly. ]

来源:https://stackoverflow.com/questions/13190743/compiler-added-optimization-causes-different-behavior-for-final-methods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!