(1)、成員函數
成員函數有一個非成員函數不具有的屬性——它的類itsclass 指向成員函數的指針必須與向其賦值的函數類型匹配不是兩個而是三個方面都要匹配:
1 參數的類型和個數2 返回類型3 它所屬的類類型
例如類screen:short Screen::*ps_Screen = &Screen::_height;
數據成員指針在被用來訪問數據成員之前必須先被綁定到一個對象或指針上
// 所有指向類成員的指針都可以用0 賦值
int (Screen::*pmf1)() = 0;
int (Screen::*pmf2)() = &Screen::height;//或者可以這樣寫:int Screen::*pmf2 = &Screen::height;
注意:靜態類成員指針是該類的全局對象和函數,引用的是普通指針
(2)作用域
1.全局域、類域、局部域的區別
int _height;
class Screen
{
public:
Screen( int _height )
{
_height = 0; // 哪一個 _height? 參數
}
private:
short _height;
};
先在函數內查找_height ,找不到再在類域查找,最后在全局域查找
可以這樣訪問:
//this->_height = 0; // 指向 Screen::_height
// 這樣也有效
// Screen::_height = 0;
::_height = 0; // 指向全局對象
2.命名空間
namespace DisneyFeatureAnimation {
class Node { /* ... */ };
}
Node *pnode; // 錯誤: Node 在全局域中不可見
// using 聲明: 使得 node 在全局域中可見
using cplusplus_primer::Node;
Node another; // cplusplus_primer::Node
3.嵌套類 :一個類可以在另一個類中定義這樣的類被稱為嵌套類
class List {
public:
class ListItem {
friend class List; // 友元聲明
ListItem( int val = 0 ); // 構造函數
ListItem *next; // 指向自己類的指針
int value;
};
// ...
private:
ListItem *list;
ListItem *at_end;
};
// ok: 全局域中的聲明
List::ListItem *headptr;
// 較好的設計!
class List {
public:
// ...
private:
// 現在 ListItem 是一個私有的嵌套類型
class ListItem
{
// 它的成員都是公有的
public:
ListItem( int val = 0 );
ListItem *next;
int value;
};
ListItem *list;
ListItem *at_end;
};
// 用外圍類名限定修飾嵌套類名 listitem的構造函數定義
List::ListItem::ListItem( int val ) {
value = val;
next = 0;
}
posted on 2011-11-30 20:33
Yu_ 閱讀(753)
評論(0) 編輯 收藏 引用 所屬分類:
C/C++