问题
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