亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

關于c++的一個有趣現象,具體看下面詳細情況?

關于c++的一個有趣現象,具體看下面詳細情況?

C++
BIG陽 2021-05-04 11:11:23
c++有趣現象:private的成員函數可以在類的外部調用?#include <iostream.h>class base;base * pbase;class base{public:base(){pbase=this;}virtual void fn(){cout<<"base"<<endl;}};class derived:public base{private:<br/> void fn()<br/> {cout<<"derived"<<endl;}};void main(){derived aa;pbase->fn();}以上程序在VC和VC7中輸出結果為 derived, 哈,居然private的成員函數可以在類的外部調用.//我的問題是,難道這是真的嗎?private的成員函數可以在類的外部調用.?//還是這個private關鍵字不應該使用?
查看完整描述

2 回答

?
慕容3067478

TA貢獻1773條經驗 獲得超3個贊

它并不是C++的缺陷或是被設計者所忽視的問題。

當我們使用虛函數的時候,它的訪問規則是在聲明的時候被確定的,而不是在被“子類重寫”(overridden)的時候,虛函數的訪問規則不會受到來自被重寫的子類函數的影響,更進一步說,當某個對象A的引用B(特指間接訪問)用來調用該對象的虛函數時,對于該對象A的一切聲明信息,都取決于該對象的引用B,而不是這個引用所引用的對象A。

C++標準里有對該問題的具體說明:當一個子類函數通過基類的指針調用時,訪問權限取決于基類對該函數的聲明。

參考C++ Standard ISO/IEC 14882:2003(E) 第11.6節:

11.6 Access to virtual functions [class.access.virt]

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. [Example:
class B {
public:
virtual int f();
};
class D : public B {
private:
int f();
};
void f()
{
D d;
B* pb = &d;
D* pd = &d;
pb->f(); //OK: B::f() is public,
// D::f() is invoked
pd->f(); //error: D::f() is private
}
—end example]

Access is checked at the call point using the type of the expression used to denote the object for which the member function is called (B* in the example above). The access of the member function in the class in which it was defined (D in the example above) is in general not known.

至少我們在程序編譯時無法獲知這個指針的值,它會指向何種位置。

經驗:private僅僅是一個訪問限定符,它只限定函數和數據不能被“直接”訪問,而不擔保這些函數和數據會被通過其他方法間接地訪問到,在成員函數中返回一個類私有數據成員的引用也是這個道理。



查看完整回答
反對 回復 2021-05-09
?
泛舟湖上清波郎朗

TA貢獻1818條經驗 獲得超3個贊

發現新大陸了呵呵,不錯,但是,它并不是C++的缺陷或是被設計者所忽視的問題。

當我們使用虛函數的時候,它的訪問規則是在聲明的時候被確定的,而不是在被“子類重寫”(overridden)的時候,虛函數的訪問規則不會受到來自被重寫的子類函數的影響,更進一步說,當某個對象A的引用B(特指間接訪問)用來調用該對象的虛函數時,對于該對象A的一切聲明信息,都取決于該對象的引用B,而不是這個引用所引用的對象A。

C++標準里有對該問題的具體說明:當一個子類函數通過基類的指針調用時,訪問權限取決于基類對該函數的聲明。

參考C++ Standard ISO/IEC 14882:2003(E) 第11.6節:

11.6 Access to virtual functions [class.access.virt]

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. [Example:
class B {
public:
virtual int f();
};
class D : public B {
private:
int f();
};
void f()
{
D d;
B* pb = &d;
D* pd = &d;
pb->f(); //OK: B::f() is public,
// D::f() is invoked
pd->f(); //error: D::f() is private
}
—end example]

Access is checked at the call point using the type of the expression used to denote the object for which the member function is called (B* in the example above). The access of the member function in the class in which it was defined (D in the example above) is in general not known.

至少我們在程序編譯時無法獲知這個指針的值,它會指向何種位置。

經驗:private僅僅是一個訪問限定符,它只限定函數和數據不能被“直接”訪問,而不擔保這些函數和數據會被通過其他方法間接地訪問到,在成員函數中返回一個類私有數據成員的引用也是這個道理。

另外,如果不是特殊的需要,一般來說,這并不是一個好的設計,有點自找麻煩的味道!



查看完整回答
反對 回復 2021-05-09
  • 2 回答
  • 0 關注
  • 260 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號