網站首頁 編程語言 正文
下面看下成員解除引用運算符,C++允許定義指向類成員的指針,對這種指針進行聲明或解除引用時,需要使用一種特殊的表示法。
例:
class Example { private: int feet; int inches; public: Example(); Example(int ft); ~Example(); void show_in()const; void show_ft()const; void use_ptr()const; };
如果沒有具體的對象,則inches成員只是一個標簽。也就是說,這個類將inches定義為一個成員標識符,但要為它分配內存。必須聲明這個類的一個對象:
Example ob;//現在ob.inches存在
因此,可以結合使用標識符inches和特定的對象,來引用實際的內存單元(對于成員函數,可以省略對象名,但對象被認為是指針執行對象)。
C++允許這樣定義一個指向標識符inches的成員指針:
int Example::*pt = &Example::inchers;
這種指針與常規指針有所區別。常規指針指向特定的單元格,而pt指針并不是指向特定的內存單元,因為聲明中沒有指出具體的對象。指針pt指的是inches成員在任意Example對象中的位置。和標識符inches一樣,pt被設計與對象標識符要求使用。實際上。表達式*pt對標識符inches的角色做了假設,因此,可以使用對象標識符來指定訪問的對象,使用pt指針來指定該對象的inches成員。例如:類方法可以使用下面得的代碼:
int Example::*pt = &Example::inches; Example ob1; Example ob2; Example *pq = new Example; cout<<ob1.*pt<<endl;//ob1對象的inches成員 cout<<ob2.*pt<<endl;//ob2對象的inches成員 cout<<po->*pt<<endl;//*po對象的inches成員
其中,*和->*都是成員解除運算符,聲明對象后,ob1.*pi指的將是ob1對象的inches成員,同樣,pq->*pt指的是pq指向的對象的inxhes成員。
改變上述示例中使用的對象,將改變使用的inches成員。不過也可以修改pt指針本身。由于feet的類型與inches相同,因此可以將pt重新設置為指向feet成員(而不指向inches成員),這樣ob1.*pt將是ob1的feet成員:
pt = &Example::feet; cout<<ob1.*pt<<endl;//*pt相當于成員名,可用標識(相同類型)其他成員
可以使用成員指針標識成員函數,其語法稍微復雜的。對于不帶任何參數、返回值為void的函數,聲明一個指向函數的指針:
void (*pf)();//pf 指向函數
聲明指向成員函數指針時,必須指出該函數所屬的類。例:
void (Example::*pf)()const;//pf指向類成員函數
表明pf可用于使用Example方法地方。且Example::*pf必須放在括號中,可以將特定成員函數的地址賦給指針:
pf = &Example::show_inches;
注意,與普通函數指針的賦值情況不同,這里必須使用地址運算符,完成賦值操作后,便可以使用一個對象來調用該成員函數:
Example ob3(20); (ob3.*pf)();//使用ob3對象調用show_feet()
必須將ob3*p放在括號中,以明確地指出,該表達式表示的是一個函數名。
由于show_feet()原型與show_inches()相同,因此也可以使用pf來訪問show_feet()方法:
pf = &Example::show_feet; (ob3*pf)();//將show_feet()應用于ob3對象
例:
下面程序use_ptr()方法,使用成員指針來訪問Example類的數據成員和函數成員。
#include <iostream> using namespace std; class Example { private: int feet; int inches; public: Example(); Example(int ft); ~Example(); void show_in()const; void show_ft()const; void use_ptr()const; }; Example::Example() { feet=0; inches=0; } Example::Example(int ft) { feet=ft; inches=12*feet; } Example::~Example() { } void Example::show_in()const { cout<<inches<<"inches\n"; } void Example::show_ft()const { cout<<feet<<"feet\n"; } void Example::use_ptr()const { Example yard(3); int Example::*pt; pt=&Example::inches; cout<<"Set pt to &Example::inches:\n"; cout<<"this->pt:"<<this->*pt<<endl; cout<<"yard.*pt:"<<yard.*pt<<endl; pt=&Example::feet; cout<<"Set pt to &Example::inches:\n"; cout<<"this->pt:"<<this->*pt<<endl; cout<<"yard.*pt:"<<yard.*pt<<endl; void (Example::*pf)()const; pf=&Example::show_in; cout<<"Set pf to &Example::show_in:\n"; cout<<"Using (this->*pf)():"; (this->*pf)(); cout<<"Using (yard.*pf)():"; (yard.*pf); } int main() { Example car(15); Example van(20); Example garage; cout<<"car.usr_ptr() output:\n"; car.use_ptr(); cout<<"\nvan.use_ptr()outptr:\n"; van.use_ptr(); return 0; }
本例子在編譯期間給指針賦值,在更為復雜的類中,可以使用指向數據成員和方法的成員指針。以便在運行階段確定與指針關聯的成員。
原文鏈接:https://blog.csdn.net/weixin_50866517/article/details/122353508
相關推薦
- 2022-10-18 Golang?內存管理簡單技巧詳解_Golang
- 2022-09-24 Golang?斷言與閉包使用解析_Golang
- 2022-06-06 uniApp、API ‘offCompassChange‘ is not yet implement
- 2022-07-03 C#入門之定義類成員與接口實現_C#教程
- 2022-05-25 python3?字符串str和bytes相互轉換_python
- 2022-10-09 ASP.NET?Core?5.0中的Host.CreateDefaultBuilder執行過程解析_
- 2023-03-20 C#中如何生成安裝包_C#教程
- 2023-02-03 VB十七種可用一行代碼完成判斷的技巧代碼_vb
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支