声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

声振论坛 展示 基础理论 查看内容

关于OOP中若干特性的思考

2011-3-18 16:28| 发布者: 雪缘| 查看: 1108| 评论: 0|来自: 振动论坛

摘要: 由于最近在学习Python,了解了很多所谓Python的新特性,无非是更加方便和精确地实现了面对对象(OOP)编程的精髓。有些网友甚至急匆匆地就拿Python与各种语言作比较了,我想说的是,并非C++不能这样做,是因为它不必 ...
       由于最近在学习Python,了解了很多所谓Python的新特性,无非是更加方便和精确地实现了面对对象(OOP)编程的精髓。有些网友甚至急匆匆地就拿Python与各种语言作比较了,我想说的是,并非C++不能这样做,是因为它不必这样做(关于C++的四种编程层面,请参考《Effective C++》,面对对象编程,只是其中的一种)。

       要想用C++实现这些现代面对对象编程语言的典范功能,并不需要改进语法,只需要精妙地构建一些类和宏就可以了,关于这项工作的典范,是微软出品的MFC(Microsoft Fundation Class)系列库类,设计这些库类的出发点,是将面对对象的观点引入基于windows的应用程序设计上,其亮点在于这是一个完全构建在C++之上的库类,并没有修改C++语法,实现了WINDOWS下面对对象编程所谓的“六大关键技术”,分别是:
      类层级结构(对应于Python的“万物皆对象”思想,所有的MFC类都有共同的基类:CObject类);
      运行时类型识别(对应于Python的type()函数,接受任何东西作为参数,返回的字符串告诉你这个东西是什么类别);
      动态创建(相当于Python的cPickle库或者Pickle库的load()函数,从文件中还原类的实例
      永久保存(相当于Python的cPickle库或者Pickle库的dump()函数,将类的实例保存到文件);
      消息映射(操作系统相关的技术);
      命令传递(操作系统相关的技术);

       当年在研习候俊杰的《深入浅出MFC》时,照着书上的讲解构建了一个可以实现“动态创建”和“运行时类型识别”以及“永久保存”的“类层级结构”。再贴代码之前,让我再说说这四个名词的意思吧,实际上,我认为,他们只是一个问题的四个方面
       因为我们要实现最纯粹的面对对象编程,即按照“万物皆对象”的观点,能操作的所有东西都应该是某个类的对象,而类与类之间的关系,应该组织在一张大“网”中,什么类的子类是什么,万物的源头是什么类,等等。库类框架中各类的关系,构成了类层级结构
       既然万物皆对象,我们有理由要求像保存数据一样将我们操作的对象保存在磁盘上,这就是永久保存技术,而且在需要的时候从磁盘读入内存并再现这个对象,这就是动态创建技术;这两个技术要求我们必须做到,在一个类被创建出来后,还能获知他是什么类的对象,这就是运行时类型识别技术;
       让我们来想想运行时类型识别
       例如,在Human类->Man类->Child类这样的框架中,要执行一个全局的FindAWomanToMarry(Human *p),就要先确定当前的p是什么?当然,我们知道它是Human类,子类和父类总是有Is-A关系,即Chinld类的对象是一个 Human类对象( Child IS A Human)。但是我们无法知道p表示的是Human的那个子类的对象,当然我们有其它办法例如获取年龄值等等来避开这个特定的问题,但是并没有回答我们为了说明的那个问题:怎么在一个类的对象被创建出来之后还能得到这个对象在类层级结构中的位置

    说了这么多,总而言之,我认为,运行时类型识别、动态创建、永久保存和类层级结构是面对对象语言(或框架)所必须提供的特性。MFC框架和《深入浅出MFC》告诉我们,如何在C++中构造出这出这些特性。
   
    多年前的依样画葫芦的一些代码,帖出来给大家看看吧。
    RTCT.h :包含非常精妙的宏定义,是实现运行时类型识别的关键

  1. #ifndef RTTCTRT
  2. #define RTTCTRT
  3. #define FIRST_CLASS Human
  4. #define RTTI_PRE(X)  \
  5.      static RUNTIMECLASS X##class;\
  6.      virtual RUNTIMECLASS* GetRunTimeClass() const;
  7. #define RTTI_LINK(new_class,basic_class)\
  8. static char new_class##_p[]=#new_class;\
  9. RUNTIMECLASS new_class::new_class##class={\
  10.         new_class##_p,sizeof(new_class),&basic_class::basic_class##class,NULL,NULL};\
  11. static RFX_init init##new_class(&new_class::new_class##class);\
  12. RUNTIMECLASS* new_class::GetRunTimeClass() const\
  13. {return &new_class::new_class##class;}\
  14. #define RTCT_PRE(X) \
  15.     static RUNTIMECLASS X##class;\
  16.     virtual RUNTIMECLASS* GetRunTimeClass() const;\
  17.     static Human* RUNTIMECREAT();
  18. #define RTCT_LINK(new_class,basic_class)\
  19. Human *new_class::RUNTIMECREAT()\
  20. {return new new_class; }\
  21. static char new_class##_p[]=#new_class;\
  22. RUNTIMECLASS new_class::new_class##class={\
  23. new_class##_p,sizeof(new_class),&basic_class::basic_class##class,new_class::RUNTIMECREAT,NULL};\
  24. static RFX_init init##new_class(&new_class::new_class##class);\
  25. RUNTIMECLASS* new_class::GetRunTimeClass() const\
  26. {return &new_class::new_class##class;}
  27. /////////////////////////////////////////////////
  28.     class FIRST_CLASS;
  29. /*在引用之前,这是非常必要的*/
  30. struct RUNTIMECLASS
  31. {
  32. char * name;
  33. int size;
  34. RUNTIMECLASS *basic;//指向父类
  35.     FIRST_CLASS* (*pf_Creat)();
  36.     FIRST_CLASS* RTCreat();
  37. RUNTIMECLASS *next;//指向下一节点
  38. static  RUNTIMECLASS *First;
  39. };
  40. /////////////////////////////////////////////////
  41. class RFX_init
  42. {
  43. public:
  44. RFX_init(RUNTIMECLASS *newclass);
  45. };
  46. #endif
复制代码
    UDF.h:自己构建的几个类,形成一个以Human类为源头的小框架
  1. #ifndef UDFF
  2. #define UDFF
  3. #define R_DEBUG
  4. #include "RTCT.h"
  5. int CMP(char s1[],char s2[]);
  6. class Human
  7. {
  8. public:
  9. Human(int a=1,int i=1);
  10. inline int Getage(){return age;};
  11. inline int GetID(){return age;};
  12. RTTI_PRE(Human)
  13. private:
  14. int age;
  15. int ID;
  16. };
  17. ////////////////////////////
  18. class Man:public Human
  19. {
  20. public:
  21. Man(int a=1,int i=1,int ifm=1);
  22. RTCT_PRE(Man)
  23. inline int GetMarryStatue(){return ifmarry;};
  24. private:
  25. int ifmarry;
  26. };
  27. ///////////////////////////
  28. class Woman:public Human
  29. {
  30. public:
  31. Woman(int a=1,int i=1,int ifm=1);
  32. RTCT_PRE(Woman)
  33. inline int GetMarryStatue(){return ifmarry;};
  34. private:
  35. int ifmarry;
  36. };
  37. //////////////////////////////////
  38. class Child:public Human
  39. {
  40. public:
  41. Child(int a=1,int i=1,int g=1);
  42. RTTI_PRE(Child)
  43. inline int GetGrade(){return grade;};
  44. private:
  45. int grade;
  46. };
  47. //////////////////////////////////
  48. class Baby:public Child
  49. {
  50. public:
  51. Baby(int a=1,int i=1,int g=1,int iq=1);
  52. RTTI_PRE(Baby)
  53. inline int GetIQ(){return IQ;};
  54. private:
  55. int IQ;
  56. };
  57. #endif
复制代码
    UDF.cpp:自己构建的几个类,形成一个以Human类为源头的小框架
  1. #include "UDF.h"
  2. #include <iostream.h>
  3. #include <string.h>
  4. RFX_init::RFX_init(RUNTIMECLASS *newclass)
  5. {
  6. #ifdef R_DEBUG
  7. if(RUNTIMECLASS::First!=NULL)
  8. {cout<<"First_pre"<<"["<<RUNTIMECLASS::First->name<<"]"<<endl;}
  9. else{cout<<"First_pre"<<"NULL"<<endl;}
  10.     #endif
  11. ///////////////初始化链表///////////
  12. newclass->next =RUNTIMECLASS::First;
  13.     RUNTIMECLASS::First=newclass;
  14. /////////////////////////
  15.     #ifdef R_DEBUG
  16. cout<<"["<<newclass->name<<"]"<<"LINKED"<<endl;
  17. cout<<"First_"<<"["<<RUNTIMECLASS::First->name<<"]"<<endl;
  18. cout<<"*****************************"<<endl<<endl;
  19.     #endif
  20. }
  21. RUNTIMECLASS* RUNTIMECLASS::First =NULL;
  22. static char Human_p[]="Human";
  23. RUNTIMECLASS Human::Humanclass={Human_p,sizeof(Human),NULL,NULL,NULL};
  24. RFX_init initHuman(&Human::Humanclass);
  25. RUNTIMECLASS* Human::GetRunTimeClass() const
  26. {return &Human::Humanclass;}
  27. Human::Human(int a,int i)
  28. {
  29. cout<<"Human Creat."<<endl;
  30. age=a;
  31. ID=i;
  32. }
  33. Man::Man(int a,int i,int ifm):Human(a,i)
  34. {
  35. cout<<"Man Creat."<<endl;
  36. ifmarry=ifm;
  37. }
  38. Woman::Woman(int a,int i,int ifm):Human(a,i)
  39. {
  40. cout<<"Woman Creat."<<endl;
  41. ifmarry=ifm;
  42. }
  43. Child::Child(int a,int i,int g):Human(a,i)
  44. {
  45. cout<<"Child Creat."<<endl;
  46. grade=g;
  47. }
  48. Baby::Baby(int a,int i,int g,int iq):Child(a,i,g)
  49. {
  50. cout<<"Baby Creat."<<endl;
  51. IQ=iq;
  52. }
  53. Human* RUNTIMECLASS::RTCreat()
  54. {
  55. if(pf_Creat==NULL)
  56. {
  57.   cout<<"此子类不支持动态创建。"<<endl;
  58.   return NULL;
  59. }
  60. else
  61. {
  62.   return (*pf_Creat)();
  63. }
  64. }
  65. int CMP(char s1[],char s2[])
  66. {
  67. int i=0;
  68. if (strlen(s1)!=strlen(s2))
  69.      return 0;
  70. for(;s1[i]!='\0';i++)
  71. {
  72.   if(s1[i]!=s2[i])
  73.    return 0;
  74. }
  75. return 1;
  76. }
  77. RTTI_LINK(Baby,Child)
  78. RTCT_LINK(Man,Human)
  79. RTCT_LINK(Woman,Human)
  80. RTTI_LINK(Child,Human)
复制代码
    main.cpp:通过往这个框架中添加一个类来说明如何使用这些特性
  1. #include <iostream.h>
  2. #include <stdlib.h>
  3. #include "UDF.h"

  4. class Rainyboy:public Baby
  5. {
  6. public:
  7. Rainyboy(int a=1,int i=1,int g=1,int iq=1);
  8. ~Rainyboy();
  9. RTCT_PRE(Rainyboy)
  10. };
  11. Rainyboy::Rainyboy(int a,int i,int g,int iq):Baby(a,i,g,iq)
  12. {
  13. cout<<"Rainyboy Creat."<<endl;
  14. }
  15. Rainyboy::~Rainyboy(){cout<<"Rainyboy Broken";};
  16.     RTCT_LINK(Rainyboy,Baby)
  17. int main()
  18. {
  19. RUNTIMECLASS *pnow;
  20. RUNTIMECLASS *pbas;
  21. Human *newp;
  22. char s[10];
  23. cout<<"**********"<<endl<<"类别目录表"<<endl<<"**********"<<endl;
  24. for(pnow=RUNTIMECLASS::First;pnow!=NULL;pnow=pnow->next)
  25. {
  26.   cout<<pnow->name<<"["<<pnow->size<<"]";
  27.   for(pbas=pnow->basic;pbas!=NULL;pbas=pbas->basic)
  28.    cout<<"<-"<<pbas->name<<"["<<pbas->size<<"]";
  29.         cout<<endl;
  30. }
  31. cout<<"***********************************"<<endl;
  32. cout<<"动态创建测试,输入要创建的类名:";
  33. cin>>s;
  34. cout<<"***********************************"<<endl;
  35. for(pnow=RUNTIMECLASS::First;pnow!=NULL;pnow=pnow->next)
  36. {
  37.   if(CMP(pnow->name,s))
  38.   {
  39.    cout<<"Class "<<pnow->name<<"  FOUNED!"<<endl;
  40.    newp=pnow->RTCreat();
  41.    break;
  42.   }
  43. }
  44. if(pnow==NULL)
  45.   cout<<"Class "<<s<<" NOT FOUNED!"<<endl;
  46. delete newp;
  47. return 1;
  48. }
复制代码

      程序运行之后,显示出这些信息:
      [attach]47384[/attach]
      输入要创建的类名后,显示:
      [attach]47385[/attach]
      说明动态创建成功。

      PS:花了些时间写了这个有点长的东西,是因为随着学习Python的推进,觉得很多东西都是似曾相识,是已有技术的另一种实现。因此翻出这些老古董代码,整理自己的思路,就当是抛砖引玉吧,呵呵!

      PS2:《深入浅出MFC》是一本不可多得的好书,今日再读,更觉得它是求知路上不可多得的良师益友,全书四篇:“勿在浮沙筑高台”,“欲善工事先利其器”,“浅出MFC程序设计”,“深入MFC程序设计”,不急不躁,带领读者慢慢品味代码之美,编程之乐,令人流连忘返!

本文内容由 Rainyboy 提供

最新评论

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-5-20 14:04 , Processed in 0.033425 second(s), 15 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部