亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C++成員解除引用運算符的示例詳解

 更新時間:2022年01月08日 15:29:49   作者:D@@  
這篇文章主要介紹了C++成員解除引用運算符,本例子在編譯期間給指針賦值,在更為復雜的類中,可以使用指向數(shù)據(jù)成員和方法的成員指針,需要的朋友可以參考下

下面看下成員解除引用運算符,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被設計與對象標識符要求使用。實際上。表達式*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相當于成員名,可用標識(相同類型)其他成員

可以使用成員指針標識成員函數(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()應用于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)文章

最新評論