C++ 语言中,为什么私有(private)函数有时能被外部访问、调用? |
您所在的位置:网站首页 › 洪荒之阐教仙 › C++ 语言中,为什么私有(private)函数有时能被外部访问、调用? |
c++标准 11.6中,有如下段描述: The access rules (clause 11) for a virtual function are determined by its declaration and are not affected by the rules for a function that later overrides it也就是说访问规则由声明决定,这句话的理解就是, A *a = new B; a->get();是由A对象里面的get的访问权限决定,因为A::get是public,所以是可以访问的 而如果换成 B *b = new B; b->get();这样就错了,因为B::get()是private的 而对于 A *a = new B; a->get()这块就涉及到虚函数表了,当然虚函数表是在运行时态,B::get在虚函数表中,a只需要知道其虚函数表的地址就能访问,而这个时候,是不需要访问控制检测的,即 是否为 为 public 或者 private Access control is implemented at compile time, not run-time, while polymorphism (including the use of virtual functions) is a run-time feature.当然,也可以这么理解:访问控制检测是在编译器,而不是运行期 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |