1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | #include <iostream> //~ #include <string> using namespace std; class Base { public: virtual void foo() { cout<<"Base::foo()"<<endl; } }; class Derived: public Base { private: void foo() { cout<<"Derived::foo()"<<endl; } }; int main() { Derived * pD = new Derived; Base * pB = pD; pB->foo(); //~ OK, call Derived::foo() //~ pD->foo(); //~ oops, Derived::foo() is private! return 0; } |
可见,权限检查发生在名字查找之时,而虚函数表的访问不带有权限检查。
