『C++』Temp_2018_12_13_Type

 

 

  1 #include <iostream>
  2 #include <string>
  3 using namespace std;
  4 
  5 class Object{
  6 public:
  7     virtual void ToString(){
  8         cout<<"Object"<<endl;
  9     }
 10 };
 11 
 12 class Test : public Object{
 13 private:
 14     string name;
 15 public:
 16     string GetName(){
 17         return name;
 18     }
 19     void SetName(string name){
 20         this->name=name;
 21     }
 22 
 23 public:
 24     void ToString(){
 25         cout<<"Test"<<endl;
 26     }
 27 
 28 public:
 29     static Object* Call(Object* obj){
 30         Test* test = new Test();
 31         return test;
 32     }
 33 };
 34 
 35 class Test22 : public Object{
 36 public:
 37     void ToString(){
 38         cout<<"Test22"<<endl;
 39     }
 40 
 41 public:
 42     static Object* Call(Object* obj){
 43         Test22* test = new Test22();
 44         return test;
 45     }
 46 };
 47 
 48 
 49 
 50 typedef Object* (*MethodPtr)(Object*);
 51 
 52 
 53 class Method{
 54 public:
 55     string Name;
 56     MethodPtr MethodPtr; 
 57     Object* Invoke(Object* arg){
 58         Object* obj = MethodPtr(arg);
 59         return obj;
 60     }
 61 };
 62 
 63 
 64 
 65 
 66 class Type{
 67 public:
 68     MethodPtr MPtr; 
 69     Type(MethodPtr ptr){
 70         MPtr=ptr;
 71     }
 72 
 73 public:
 74     Method* GetCtor(){
 75         Method* method=new Method();
 76         method->Name=".ctor";
 77         method->MethodPtr = MPtr;
 78         return method;
 79     }
 80 };
 81 
 82 //typedef Object (__stdcall Method::*CallPtr)(Object);
 83 
 84 
 85 int main(){
 86     cout<<"----------------"<<endl;
 87 
 88     Type* type = new Type(&Test::Call);
 89     Method*  method = type->GetCtor();
 90     Object* obj = method->Invoke(NULL);
 91     obj->ToString();
 92 
 93 
 94     Type* type2 = new Type(&Test22::Call);
 95     Method*  method2 = type2->GetCtor();
 96     Object* obj2 = method2->Invoke(NULL);
 97     obj2->ToString();
 98 
 99 
100     cout<<"----------------"<<endl;
101     cin.get();
102 }

 

1 ----------------
2 Test
3 Test22
4 ----------------

 

posted @ 2018-12-13 19:27  redc  阅读(142)  评论(0编辑  收藏  举报