Public virtual function derived private in C++

前端 未结 3 722
离开以前
离开以前 2020-12-04 22:36

I was trying to figure out what happens when a derived class declares a virtual function as private. The following is the program that I wrote

#include 

        
3条回答
  •  眼角桃花
    2020-12-04 23:01

    This is well-defined behavior. If a were a B* this wouldn't compile. The reason is that member access is resolved statically by the compiler, not dynamically at run-time. Many C++ books suggest that you avoid coding like this because it confuses less experienced coders.

提交回复
热议问题