关键词搜索

源码搜索 ×
×

对象间是如何进行勾搭的?

发布2014-03-16浏览7808次

详情内容

       什么是对象?在int a; 中, a就是对象。那么如何与对象a进行通信呢? 简单啊,且看程序:

  1. #include <iostream>
  2. using namespace std;
  3. int a;
  4. int main()
  5. {
  6. a = 1; //往对象中写数据
  7. cout << a << endl; //从对象中读取数据
  8. return 0;
  9. }


      继续上菜,爱吃不吃:

  1. #include <iostream>
  2. using namespace std;
  3. class A
  4. {
  5. private:
  6. int x;
  7. static A *pInstance;
  8. public:
  9. void set(int m)
  10. {
  11. x = m;
  12. }
  13. int get()
  14. {
  15. return x;
  16. }
  17. static A *getInstance()
  18. {
  19. static A* pInstance = NULL;
  20. if(NULL == pInstance)
  21. {
  22. pInstance = new A;
  23. }
  24. return pInstance;
  25. }
  26. };
  27. int main()
  28. {
  29. A::getInstance()->set(100);
  30. cout << A::getInstance()->get() << endl;
  31. //A::getInstance()指向一个单例
  32. return 0;
  33. }

 

        继续上菜:

  1. #include <iostream>
  2. using namespace std;
  3. class A
  4. {
  5. private:
  6. int x;
  7. static A *pInstance;
  8. public:
  9. void set(int m)
  10. {
  11. x = m;
  12. }
  13. int get()
  14. {
  15. return x;
  16. }
  17. static A *getInstance()
  18. {
  19. static A* pInstance = NULL;
  20. if(NULL == pInstance)
  21. {
  22. pInstance = new A;
  23. }
  24. return pInstance;
  25. }
  26. };
  27. class B
  28. {
  29. public:
  30. void fun1()
  31. {
  32. A::getInstance()->set(100);
  33. }
  34. void fun2()
  35. {
  36. cout << A::getInstance()->get() << endl;
  37. }
  38. };
  39. int main()
  40. {
  41. B b;
  42. b.fun1();
  43. b.fun2();
  44. return 0;
  45. }


      由此可见,对象间的勾搭是通过接口来起作用的,无非就是入和出。

相关技术文章

最新源码

下载排行榜

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载