本文介紹了如何在不交“朋友"的情況下訪問類外的私有數據成員?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我有一個 class A
如下所述:-
I have a class A
as mentioned below:-
class A{
int iData;
};
我既不想創建成員函數,也不想繼承上面的class A
,也不想改變iData
的說明符.
I neither want to create member function nor inherit the above class A
nor change the specifier of iData
.
我的疑問:-
- 如何訪問
iData
對象的obj1
,它是class A
的一個實例? - 如何更改或操作對象
obj1
的iData
?
- How to access
iData
of an object sayobj1
which is an instance ofclass A
? - How to change or manipulate the
iData
of an objectobj1
?
注意:不要使用friend
.
推薦答案
你不能.該成員是私有的,在課堂外不可見.這就是 public/protected/private 修飾符的全部意義.
You can't. That member is private, it's not visible outside the class. That's the whole point of the public/protected/private modifiers.
(雖然你可能會使用臟指針技巧,但我猜你會很快進入未定義的行為領域.)
(You could probably use dirty pointer tricks though, but my guess is that you'd enter undefined behavior territory pretty fast.)
這篇關于如何在不交“朋友"的情況下訪問類外的私有數據成員?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!