C++(11):友元、嵌套类
来源:互联网 发布:java中怎么求绝对值 编辑:程序博客网 时间:2024/06/04 21:16
友元(Friend)
友元函数(Friend Function)
But C++ also provides another of function to access members in class friend functions
– friend functions are not member functions but can still access class private members
– defined outside of class scope
– to access private members of two or more different classes
– for I/O or operator functions
– placed inside the class definition and preceded by the friend keyword
– defined outside the class as a normal, nonmember function
– called like a normal non-member function.
If a function is a friend of two or more different classes, each class must contain the friend function prototype within its body.
A friend function cannot be inherited but can be a member of one class.
注意。
1. 类内声明(要加friend修饰词),类外定义(不能加friend修饰词)
2. 不能被继承。
3. 友元函数不是成员函数!使用的时候,一定不能写 obj1.myFriendFun1();!因为不是成员函数!点不出来的!
友元类(Friend Classes)
An entire class can be a friend of another class
– can be used when all member functions of one class should access the data of another class
– require prototypes to be placed within each class individually
An entire class can be designed as friend
– all its member functions automatically granted a friendship by the class
– but “class B is a friend of class A” does not imply “class A is a friend of class B”
//in CPoint.hclass CPoint {friend class CLine; // 我(CPoint)把你(CLine)当朋友,你就可以取到我的东西int x, y;void Offset(int diff) {x += diff; y += diff;}public:CPoint() { x=0; y=0; }CPoint(int a, int b) { x=a; y=b; }void set(int a, int b) {x=a; y=b;} void Print() {cout << x << “ ” << y << endl;}};
//in CLine.hclass CLine {CPoint p1, p2;public:CLine(int x, int y, int w, int z) {p1.x = x; p1.y = y; //access privatep2.x = w; p2.y = z; }void Print()() {//call public Print in CPointcout << “Point 1:”; p1.Print();cout << “Point 2:”; p2.Print(); }void Display() {offset(p1,100); //call friend funcp2.Offset(200); //call private funcPrint(p1, p2); }};友元类,好像现在看起来,发明者提供这种写法,好像也只是为了让代码的写作更加简单……
e.g. A为了访问B中的成员变量,就用set和get不就好了吗……
嵌套类(Nested Class)
感觉有点像内部类。我在做Android开发的时候,还遇到过匿名内部类……
e.g. xxxButton.setOnClickListener(new xxxListener() {
@override
void xxxOnClick(Event e) {
xxx;
}
});
这种…也不知道记错没有……感觉好像是这样…………
Nested class cannot access any private member of the outer class by default.
嵌套类默认情况下,不能访问外部的类的私有成员。但,可以用friend解决。
e.g. 在外部类A中声明嵌套类B是它的朋友,friend class B;
Inner class is not visible globally.
A nested class CTwo in COne can have the same kind of members as a nonnested class.
链表的定义,一种递归的定义。
class Chain { //outer classpublic:class Item { //all members are privatefriend class Chain;Item(int val = 0); //constructorItem *next; //point to its own classint val;};//...private:Item *chain;Item *at_end;};
注意,在List中,这个Item * next不是自己指向自己,而是说,可以指向这个相同class的实例!
(当然,也可以自己指向自己…)
- C++(11):友元、嵌套类
- 嵌套友元
- C++ 的嵌套友元
- 【ThinkingInC++】31、嵌套友元
- C++之内部类与外部类(嵌套类)及友元
- C++之嵌套类与外围类及友元
- 【C++ Primer】第十五章 友元、异常和其他 --之一--->友元和嵌套类
- c++-类的友元
- 友元函数(C++)
- C++-友元(friend)
- C++(5)/友元
- 【C/C++】嵌套类
- 友元函数与友元类、友元与嵌套
- 一步一步学习C++(类)之友元函数
- 继承与嵌套类(C#)
- C++_嵌套类
- 函数嵌套(c++)
- C++(21)友元、static成员
- iOS开发 - 滑动控制屏幕亮度和系统音量(附加AVAudioPlayer基本用法和Masonry简单使用)
- 如何防御SQL注入
- Base64系列第三篇 python中使用Base64编码解码
- GUI系统总结
- 工厂模式(简单工厂、工厂方法、抽象工厂)
- C++(11):友元、嵌套类
- sublime Text3 插件编写教程_第一课
- ORA-38856: cannot mark instance UNNAMED_INSTANCE_2 (redo thread 2) as enabled
- jquery on绑定多个事件
- Linux下fork创建进程的一些思考
- 2016 年 CSS 库、框架和工具新生榜 TOP 50
- 生成HTTPS签名证书,IOS可用
- Java中equals和equalsignorecase区别
- url参数返回乱码问题