C++ Gossip: 巢狀類別(Nested Classes)
在巢狀類別結構中,外部類別不能存取內部類別的私用成員,如果想要存取內部類別的私用成員的話,必須宣告外部類別為friend,例如:
发布日期:2022-02-10 13:35:52
浏览次数:22
分类:技术文章
本文共 3429 字,大约阅读时间需要 11 分钟。
類別可以定義在另一個類別之中,這樣的類別稱之為巢狀類別或內部類別,內部類別只被外部包裹的類別所見,當某個Slave類別完全只服務於一個 Master類別時,您可以將之設定為內部類別,如此使用Master類別的人就不用知道Slave的存在。 一個巢狀類別通常宣告在"private"區域,也可以宣告在"protected"或"public"區域,一個宣告的例子如下: class OuterClass { private: class InnerClass { // .... }; };
以下是個巢狀類別的簡單示範: - PointDemo.h
class PointDemo {public: PointDemo(int); ~PointDemo(); void show();private: // Nested Class class Point { public: Point(); Point(int, int); int x() { return _x; } int y() { return _y; } void x(int x) { _x = x; } void y(int y) { _y = y; } private: int _x; int _y; }; Point **_points; int _length;};
- PointDemo.cpp
#include
#include "PointDemo.h"using namespace std;// 實作內部類別PointDemo::Point::Point() { _x = 0; _y = 0;}// 實作內部類別PointDemo::Point::Point(int x, int y) { _x = x; _y = y;}PointDemo::PointDemo(int length) : _length(length) { _points = new Point*[_length]; for(int i = 0; i < _length; i++) { _points[i] = new Point(); _points[i]->x(i*5); _points[i]->y(i*5); } }void PointDemo::show() { for(int i = 0; i < _length; i++) { cout << "(x, y) = (" << _points[i]->x() << ", " << _points[i]->y() << ")" << endl; } }PointDemo::~PointDemo() { for(int i = 0; i < _length; i++) { delete _points[i]; } delete [] _points;}
- main.cpp
#include
#include "PointDemo.h"using namespace std;int main() { PointDemo demo(10); demo.show(); return 0;}
(x, y) = (0, 0) (x, y) = (5, 5) (x, y) = (10, 10) (x, y) = (15, 15) (x, y) = (20, 20) (x, y) = (25, 25) (x, y) = (30, 30) (x, y) = (35, 35) (x, y) = (40, 40) (x, y) = (45, 45) |
在巢狀類別結構中,外部類別不能存取內部類別的私用成員,如果想要存取內部類別的私用成員的話,必須宣告外部類別為friend,例如:
class PointDemo {
...
private: // Nested Class
class Point { friend class PointDemo;
....
};
....
};
同樣的,內部類別不可存取外部類別的私用成員,如果要存取私用成員的話,必須宣告其為friend,例如: ...
private: // Nested Class
class Point { friend class PointDemo;
....
};
....
};
class PointDemo {
public:
...
friend class Point;
private:
// Nested Class
class Point {
....
};
....
};
存取外部類別的非靜態成員時,必須透過物件、指標或是參考,而不是直接呼叫。 您也可以將內部類別獨立定義在一個檔案中,例如: public:
...
friend class Point;
private:
// Nested Class
class Point {
....
};
....
};
- PointDemo.h
class PointDemo {public: PointDemo(int); ~PointDemo(); void show();private: // Nested Class class Point; Point **_points; int _length;};
- Point.h
class PointDemo::Point {public: Point(); Point(int, int); int x() { return _x; } int y() { return _y; } void x(int x) { _x = x; } void y(int y) { _y = y; }private: int _x; int _y;};
- PointDemo.cpp
#include#include "PointDemo.h"#include "Point.h"using namespace std;PointDemo::PointDemo(int length) : _length(length) { _points = new Point*[_length]; for(int i = 0; i < _length; i++) { _points[i] = new Point(); _points[i]->x(i*5); _points[i]->y(i*5); } }void PointDemo::show() { for(int i = 0; i < _length; i++) { cout << "(x, y) = (" << _points[i]->x() << ", " << _points[i]->y() << ")" << endl; } }PointDemo::~PointDemo() { for(int i = 0; i < _length; i++) { delete _points[i]; } delete [] _points;}
- Point.cpp
#include "PointDemo.h"#include "Point.h"PointDemo::Point::Point() { _x = 0; _y = 0;}PointDemo::Point::Point(int x, int y) { _x = x; _y = y;}原文出处:http://openhome.cc/Gossip/CppGossip/NestedClasses.html
转载地址:https://blog.csdn.net/zoopang/article/details/20155667 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年04月20日 17时39分56秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
如何使得按确定和取消按纽转到两个不同的页面!
2019-04-27
DataGrid 的 全选/取消全选 控制(CheckBox)
2019-04-27
屏蔽相应键盘按钮
2019-04-27
事半功倍系列 javascript
2019-04-27
MIS开发中.net Framework的打印功能
2019-04-27
虚拟主机上用Asp.net实现Urlrewrite
2019-04-27
ASP.NET 应用中大文件上传研究
2019-04-27
如何最大限度提高.NET的性能
2019-04-27
短信收发类无错版JustinIO.cs
2019-04-27
短信编码类无错版PDUdecoding.cs
2019-04-27
CDMA模块上网设置的过程
2019-04-27
WISMO模块GPRS上网设置的过程
2019-04-27
简要AT命令介绍
2019-04-27
短信收发类无错版SerialStream.cs
2019-04-27
短信猫GSM Modem
2019-04-27
HTTP Response中的Chunked编码
2019-04-27
ASP.NET弹出窗口技术之增加网站流量方法
2019-04-27
IOCP , kqueue , epoll ... 有多重要?
2019-04-27
ASP.NET设计应用程序的七大绝招
2019-04-27