C++成員解除引用運算符的示例詳解
下面看下成員解除引用運算符,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定義為一個成員標識符,但要為它分配內(nèi)存。必須聲明這個類的一個對象:
Example ob;//現(xiàn)在ob.inches存在
因此,可以結(jié)合使用標識符inches和特定的對象,來引用實際的內(nèi)存單元(對于成員函數(shù),可以省略對象名,但對象被認為是指針執(zhí)行對象)。
C++允許這樣定義一個指向標識符inches的成員指針:
int Example::*pt = &Example::inchers;
這種指針與常規(guī)指針有所區(qū)別。常規(guī)指針指向特定的單元格,而pt指針并不是指向特定的內(nèi)存單元,因為聲明中沒有指出具體的對象。指針pt指的是inches成員在任意Example對象中的位置。和標識符inches一樣,pt被設(shè)計與對象標識符要求使用。實際上。表達式*pt對標識符inches的角色做了假設(shè),因此,可以使用對象標識符來指定訪問的對象,使用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重新設(shè)置為指向feet成員(而不指向inches成員),這樣ob1.*pt將是ob1的feet成員:
pt = &Example::feet; cout<<ob1.*pt<<endl;//*pt相當于成員名,可用標識(相同類型)其他成員
可以使用成員指針標識成員函數(shù),其語法稍微復雜的。對于不帶任何參數(shù)、返回值為void的函數(shù),聲明一個指向函數(shù)的指針:
void (*pf)();//pf 指向函數(shù)
聲明指向成員函數(shù)指針時,必須指出該函數(shù)所屬的類。例:
void (Example::*pf)()const;//pf指向類成員函數(shù)
表明pf可用于使用Example方法地方。且Example::*pf必須放在括號中,可以將特定成員函數(shù)的地址賦給指針:
pf = &Example::show_inches;
注意,與普通函數(shù)指針的賦值情況不同,這里必須使用地址運算符,完成賦值操作后,便可以使用一個對象來調(diào)用該成員函數(shù):
Example ob3(20); (ob3.*pf)();//使用ob3對象調(diào)用show_feet()
必須將ob3*p放在括號中,以明確地指出,該表達式表示的是一個函數(shù)名。
由于show_feet()原型與show_inches()相同,因此也可以使用pf來訪問show_feet()方法:
pf = &Example::show_feet; (ob3*pf)();//將show_feet()應(yīng)用于ob3對象
例:
下面程序use_ptr()方法,使用成員指針來訪問Example類的數(shù)據(jù)成員和函數(shù)成員。
#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;
}本例子在編譯期間給指針賦值,在更為復雜的類中,可以使用指向數(shù)據(jù)成員和方法的成員指針。以便在運行階段確定與指針關(guān)聯(lián)的成員。

到此這篇關(guān)于C++成員解除引用運算符的示例詳解的文章就介紹到這了,更多相關(guān)C++解除引用運算符內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++中priority_queue與仿函數(shù)實現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于C++中priority_queue與仿函數(shù)實現(xiàn)的相關(guān)資料,優(yōu)先級隊列是一種容器適配器,其底層通常采用vector容器,并通過堆算法來維護元素的順序,文中通過代碼介紹的非常詳細《》需要的朋友可以參考下2024-10-10
C++ 中const對象與const成員函數(shù)的實例詳解
這篇文章主要介紹了C++ 中const對象與const成員函數(shù)的實例詳解的相關(guān)資料,希望通過本文能讓大家徹底掌握該如何使用,需要的朋友可以參考下2017-08-08
VC程序設(shè)計中CreateProcess用法注意事項
這篇文章主要介紹了VC程序設(shè)計中CreateProcess用法注意事項,需要的朋友可以參考下2014-07-07
C++實現(xiàn)inline hook的原理及應(yīng)用實例
這篇文章主要介紹了C++實現(xiàn)inline hook的原理及應(yīng)用,需要的朋友可以參考下2014-08-08
Visual Studio添加第三方庫的實現(xiàn)步驟
使用Visual Studio編寫C語言程序能夠提供全面而強大的開發(fā)環(huán)境,本文主要介紹了Visual Studio添加第三方庫的實現(xiàn)步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-07-07

