37、C++ Primer 4th筆記,特殊工具與技術,類成員指標

來源:互聯網
上載者:User

1、成員指標(pointer to member)包含類的類型以及成員的類型。成員指標只應用於類的非static成員。static類成員不是任何對象的組成部分,所以不需要特殊文法來指向static成員,static成員指標是普通指標。通過指定函數的傳回型別,形參表(類型和數目,是否為const)和所屬類來定義成員函數的指標。

2、使用類成員的指標

    類似於成員訪問操作符 . 和 ->,.* 和 -> 是兩個新的操作符,它們使我們能夠將成員指標綁定到實際對象。這兩個操作符的左運算元必須是類類型的對象或類類型的指標,右運算元是該類型的成員指標。

• 成員指標解引用操作符(.*)從對象或引用擷取成員。

• 成員指標箭頭操作符(->*)通過對象的指標擷取成員。

範例程式碼

#include "iostream"#include "string"#include "vector"using namespace std;class Screen{public:Screen(std::string StrCon = "Here", std::string::size_type myCursor = 12):contents(StrCon), cursor(myCursor){}typedef std::string::size_type index;char get() const{return '1';};char get(index ht, index wd) const{return '2';};public:std::string contents;index cursor;index height, width;};int main(){//定義的成員指標從右向左讀string Screen::*ps_Screen = &Screen::contents; char (Screen::*pmf)() const = &Screen::get;char (Screen::*pmf1)(Screen::index,Screen::index) const = &Screen::get;Screen myScreen;//使用成員函數的指標char c1 = myScreen.get();char c2 = (myScreen.*pmf)();cout << c1 << " " << c2 << endl; // 1  1Screen *pScreen = &myScreen;c1 = pScreen->get();c2 = (pScreen->*pmf)();cout << c1 << " " << c2 << endl; // 1  1c1 = pScreen->get(0, 0);c2 = (pScreen->*pmf1)(0, 0);cout << c1 << " " << c2 << endl; // 2  2//使用資料成員的指標Screen::index Screen::*pIndex = &Screen::cursor;Screen::index ind1 = myScreen.cursor;Screen::index ind2 = myScreen.*pIndex;cout << ind1 << " " << ind2 << endl;return 1;}

注意: (myScreen.*pmf)();不能省略括弧。因為()的優先順序比*高,所以如果省略括弧,則解析成:myScreen.*(pmf());這段代碼的意思是:調用名為pmf的函數,把函數的傳回值綁定到成員對象操作符(.*)的指標。

2)成員指標函數表

函數指標和成員函數指標的一個公用用途是,將它們儲存在函數表中。函數表是函數指標的集合,在運行時從中選擇給定調用。

範例程式碼

#include "iostream"#include "string"#include "vector"using namespace std;class Screen {public:// other interface and implementation members as before//Screen& home(){}; // cursor movement functions//Screen& forward(){};//Screen& back(){};//Screen& up(){};//Screen& down(){};int home() {return 1;}int forward(){return 1;}int back() {return 1;}int up(){return 1;}int down(){return 1;}public:// other interface and implementation members as before// Action is pointer that can be assigned any of the cursor movement members//typedef Screen& (Screen::*Action)();typedef int (Screen::*Action)();static Action Menu[]; // function tablepublic:// specify which direction to moveenum Directions { HOME, FORWARD, BACK, UP, DOWN };Screen& move(Directions);};Screen& Screen::move(Directions cm){// fetch the element in Menu indexed by cm// run that member on behalf of this object(this->*Menu[cm])();return *this;}Screen::Action Screen::Menu[] = { &Screen::home,&Screen::forward,&Screen::back,&Screen::up,&Screen::down,};int main(){Screen myScreen;myScreen.move(Screen::HOME); // invokes myScreen.homemyScreen.move(Screen::DOWN); // invokes myScreen.downreturn 1;}

3、枚舉的大小

範例程式碼

#include "iostream"#include "string"#include "vector"using namespace std;class A{public:enum MyData{M1, M2, M3, M4};static int iMy;};int A::iMy = 1;enum MyData{M1, M2, M3, M4};int main(){A Data1;cout << A::M1 << " " << A::M2 << " " << A::M3 << " "<< endl; //0 1 2cout << Data1.M1 << " " << Data1.M2 << " " << Data1.M3 << endl; //0 1 2//cout << A::MyData << endl; //“A::MyData”: 將此類型用作運算式非法cout << sizeof(A) << endl; //1cout << sizeof(Data1) << endl; //1cout << sizeof(MyData) << endl; //4MyData Data2;cout << sizeof(Data2) << endl; //4return 1;}

枚舉的大小為一個整形資料的大小。但是,在類中,求類的大小時,不計算資料成員的大小,同樣也不計算枚舉成員的大小。類中定義的枚舉資料成員可以通過類名和範圍直接引用。

http://www.cnblogs.com/mydomain/archive/2011/04/30/2033483.html

4、我們需要注意的是,靜態函數沒有this指標。但是,類和類對象共用一份類中定義的待用資料成員。在非靜態函數中可以通過this指標來引用這些待用資料成員。

範例程式碼

#include "iostream"#include "string"#include "vector"using namespace std;class A{public:void Print(){cout << this->iMy << endl;}public:static int iMy;};int A::iMy = 1;int main(){A Data1;    Data1.Print(); //1cout << A::iMy << endl; //1Data1.iMy = 2;A Data2;cout << Data2.iMy << endl; //2return 1;}

http://www.cnblogs.com/mydomain/archive/2011/03/22/1991449.html

5、函數指標的賦值

範例程式碼

#include <iostream>using namespace std;char myfun(){return '1';}char* myfun2(){char *p = (char*)malloc(3);return p;}int main(){char (*p)() = myfun;//char* (p1()) = &myfun2; //error,無法從“char *(__cdecl *)(void)”轉換為“char *(void)char* (*p1)() = &myfun2;//char *p() = myfun(); //注意,這樣做是錯誤的,p被定義為一個函數,返回char*,而不是函數指標。return 1;}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.