免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3292 | 回复: 5
打印 上一主题 下一主题

[C++] 从内部类调用外部类 [复制链接]

论坛徽章:
1
2015年迎新春徽章
日期:2015-03-04 09:49:03
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-04-21 14:06 |只看该作者 |倒序浏览
本帖最后由 shihyu 于 2015-04-21 14:07 编辑
  1. #include <iostream>

  2. using namespace std;

  3. class PointDemo {
  4. public:
  5.     PointDemo(int);
  6.     ~PointDemo();

  7.     void show();
  8.     void show1();
  9. private:
  10.     // Nested Class
  11.     class Point {
  12.     public:
  13.         Point();
  14.         Point(int, int);
  15.         int x() { return _x; }
  16.         int y() { return _y; }
  17.         void x(int x) { _x = x; }
  18.         void y(int y) { _y = y; }
  19.     private:
  20.         int _x;
  21.         int _y;
  22.     };

  23.     Point **_points;
  24.     int _length;
  25. };

  26. // 实作内部类别
  27. PointDemo::Point::Point() {
  28.     _x = 0;
  29.     _y = 0;
  30. }

  31. // 实作内部类别
  32. PointDemo::Point::Point(int x, int y) {
  33.     show1();
  34.     _x = x;
  35.     _y = y;
  36. }

  37. PointDemo::PointDemo(int length) : _length(length) {
  38.     _points = new Point*[_length];
  39.     for(int i = 0; i < _length; i++) {
  40.         _points[i] = new Point();
  41.         _points[i]->x(i*5);
  42.         _points[i]->y(i*5);
  43.     }
  44. }

  45. void PointDemo::show() {
  46.     for(int i = 0; i < _length; i++) {
  47.     cout << "(x, y) = ("
  48.          << _points[i]->x() << ", "
  49.          << _points[i]->y() << ")"
  50.          << endl;
  51.     }
  52. }

  53. void PointDemo::show1() {
  54.     cout << "PointDemo::show1" << endl;
  55. }

  56. PointDemo::~PointDemo() {
  57.     for(int i = 0; i < _length; i++) {
  58.         delete _points[i];
  59.     }
  60.     delete [] _points;
  61. }

  62. int main() {
  63.     PointDemo demo(10);
  64.     demo.show();
  65.     return 0;
  66. }
复制代码
test.cpp:39:11: error: cannot call member function ‘void PointDemo::show1()’ without object

要怎么改才正确?

论坛徽章:
4
双子座
日期:2014-08-28 10:08:002015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:58:112015年亚洲杯之阿联酋
日期:2015-03-13 03:25:15
2 [报告]
发表于 2015-04-21 15:16 |只看该作者
与其去讨论怎么该才能编译通过,倒不如去想你这样的设计不奇怪吗?既然定义内部类了,干嘛要去调用外部的方法

论坛徽章:
1
2015年迎新春徽章
日期:2015-03-04 09:49:03
3 [报告]
发表于 2015-04-21 19:55 |只看该作者
本帖最后由 shihyu 于 2015-04-21 19:55 编辑


在 android framework  中java code 有看到这样做法

所以好奇 c++ 能不能这样做

论坛徽章:
0
4 [报告]
发表于 2015-04-21 21:44 |只看该作者
java和C++的内部类不太一样,C++一般用组合的方式处理。

论坛徽章:
1
2015年迎新春徽章
日期:2015-03-04 09:49:03
5 [报告]
发表于 2015-04-23 11:28 |只看该作者
本帖最后由 shihyu 于 2015-04-23 11:28 编辑
  1. #include <iostream>

  2. using namespace std;

  3. class PointDemo {
  4. public:
  5.     friend class Point;
  6.     PointDemo(int);
  7.     ~PointDemo();

  8.     void show();
  9.     void show1();
  10. private:
  11.     // Nested Class
  12.     class Point {
  13.     public:
  14.         Point(PointDemo *p);
  15.         Point(int, int);
  16.         int x() { return _x; }
  17.         int y() { return _y; }
  18.         void x(int x) { _x = x; }
  19.         void y(int y) { _y = y; }
  20.     private:
  21.         int _x;
  22.         int _y;
  23.     };

  24.     Point **_points;
  25.     int _length;
  26. };

  27. // 实作内部类别
  28. PointDemo::Point::Point(PointDemo *p) {
  29.     p->show1();
  30.     _x = 0;
  31.     _y = 0;
  32. }

  33. // 实作内部类别
  34. PointDemo::Point::Point(int x, int y) {
  35.     _x = x;
  36.     _y = y;
  37. }

  38. PointDemo::PointDemo(int length) : _length(length) {
  39.     _points = new Point*[_length];
  40.     for(int i = 0; i < _length; i++) {
  41.         _points[i] = new Point(this);
  42.         _points[i]->x(i*5);
  43.         _points[i]->y(i*5);
  44.     }
  45. }

  46. void PointDemo::show() {
  47.     for(int i = 0; i < _length; i++) {
  48.     cout << "(x, y) = ("
  49.          << _points[i]->x() << ", "
  50.          << _points[i]->y() << ")"
  51.          << endl;
  52.     }
  53. }

  54. void PointDemo::show1() {
  55.     cout << "PointDemo::show1" << endl;
  56. }

  57. PointDemo::~PointDemo() {
  58.     for(int i = 0; i < _length; i++) {
  59.         delete _points[i];
  60.     }
  61.     delete [] _points;
  62. }

  63. int main() {
  64.     PointDemo demo(10);
  65.     demo.show();
  66.     return 0;
  67. }
复制代码
外部类对象传到内部类

论坛徽章:
6
技术图书徽章
日期:2013-11-13 11:11:27子鼠
日期:2014-02-20 17:54:13处女座
日期:2014-06-16 17:43:33午马
日期:2014-08-08 09:11:17未羊
日期:2014-08-10 11:57:072015年辞旧岁徽章
日期:2015-03-03 16:54:15
6 [报告]
发表于 2015-04-28 11:33 |只看该作者
本帖最后由 littledick 于 2015-04-28 11:34 编辑

如果是不需要使用本地变量的方法,定义成static的,内部类就能直接用了。
或者直接用空指针类型也能调用。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP