posts - 34,comments - 2,trackbacks - 0

(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_ 阅读(657) 评论(0)  编辑 收藏 引用 所属分类: C/C++

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理