Chinaunix首页 | 论坛 | 博客
  • 博客访问: 322191
  • 博文数量: 31
  • 博客积分: 393
  • 博客等级: 一等列兵
  • 技术积分: 388
  • 用 户 组: 普通用户
  • 注册时间: 2012-08-26 10:23
文章分类

全部博文(31)

文章存档

2013年(16)

2012年(15)

分类: C/C++

2012-10-29 13:00:35

【问题描述】
在编写对话框程序的时候,假如有三个对话框需要用户填写不同的信息,最后由主对话框点击保存退出。
在mfc中,每个对话框都是一个类,我们如何实现在主对话框点击保存,实现对用户信息的保存呢?这就要在每次用户在填写完对话框信息后,由对话框调用主对话框类的实例来实现数据的传递。

点击(此处)折叠或打开

  1. #include <iostream>

  2. using namespace std;

  3. class A
  4. {
  5. private:
  6.     int a,b;
  7.     static A * inst;

  8. public:
  9.     void set(int x,int y)
  10.     {
  11.         a=x;
  12.         b=y;
  13.     }

  14.     void show()
  15.     {
  16.         cout<<a<<" "<<b<<endl;
  17.     }

  18.     static A * getInst()
  19.     {
  20.         if(inst==NULL)
  21.         {
  22.             inst = new A;
  23.         }
  24.         return inst;
  25.     }
  26. };

  27. A* A::inst=NULL;

  28. class B
  29. {
  30. public:
  31.     void callA()
  32.     {
  33.         A * p = A::getInst();
  34.         p->set(1,1);
  35.         p->show();
  36.     }
  37. };

  38. class C
  39. {
  40. public:
  41.     void callA()
  42.     {
  43.         A * p = A::getInst();
  44.         p->set(2,2);
  45.         p->show();
  46.     }
  47. };

  48. int main()
  49. {
  50.     B b;
  51.     b.callA();
  52.     C c;
  53.     c.callA();
  54.     return 0;
  55. }
【运行结果】
1 1
2 2

阅读(3643) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~