z****e 发帖数: 2024 | 1 class Base{
public:
virtual void Method();
};
void Base::Method(){cout<<"Base::method"<
class D2:public Base{
private:
void Method(){cout<<"D2::method"<
};
void peek(Base& b){
b.Method();
}
int main(){
D2 d;
peek(d);
}
>D2::method
为什么D2虚函数是私有,但是通过一个global函数,仍然有多态?
虚函数的访问权限改变,有作用吗?
早谢。 |
N***m 发帖数: 4460 | 2 guess it is still in v-table so you can still use pointer to access it
【在 z****e 的大作中提到】 : class Base{ : public: : virtual void Method(); : }; : void Base::Method(){cout<<"Base::method"<: class D2:public Base{ : private: : void Method(){cout<<"D2::method"<: }; : void peek(Base& b){
|
t****t 发帖数: 6806 | 3 In short, access checking is static, although virtual function binding is
dynamic.
11.6 Access to virtual functions [class.access.virt]
1 The access rules (clause _class.access_) for a virtual function are
determined by its declaration and are not affected by the rules for a
function that later overrides it. [Example:
class B {
public:
virtual int f();
};
class D : public B {
private:
int f();
};
void f()
{
D d;
B* pb = &d;
【在 z****e 的大作中提到】 : class Base{ : public: : virtual void Method(); : }; : void Base::Method(){cout<<"Base::method"<: class D2:public Base{ : private: : void Method(){cout<<"D2::method"<: }; : void peek(Base& b){
|
z****e 发帖数: 2024 | 4 orz。
【在 t****t 的大作中提到】 : In short, access checking is static, although virtual function binding is : dynamic. : 11.6 Access to virtual functions [class.access.virt] : 1 The access rules (clause _class.access_) for a virtual function are : determined by its declaration and are not affected by the rules for a : function that later overrides it. [Example: : class B { : public: : virtual int f(); : };
|