• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            天下

            記錄修行的印記

            在VC2005中,只要知道類的名字,就可以動(dòng)態(tài)創(chuàng)建類的實(shí)例

            CRuntimeClass::FromName

            Call 
            this function to retrieve the CRuntimeClass structure associated with the familiar name.
             
            static CRuntimeClass* PASCAL FromName(
               LPCSTR lpszClassName 
            );
            static CRuntimeClass* PASCAL FromName(
               LPCWSTR lpszClassName 
            );
             
            Parameters
            lpszClassName
            The familiar name of a 
            class derived from CObject.

            Return Value
            A pointer to a CRuntimeClass 
            object, corresponding to the name as passed in lpszClassName. The function returns NULL if no matching class name was found.

            完整代碼如下:
            #include <iostream>
            #include 
            <afxwin.h>
            using namespace std;
            class CMyClass:
                
            public CObject
            {   
                DECLARE_SERIAL(CMyClass)
            };
            IMPLEMENT_SERIAL(CMyClass,CObject,
            1)
            //注意,CMyClass必須從CObject派生必須實(shí)現(xiàn)了DECLARE_SERIAL, IMPLEMENT_SERIAL兩個(gè)宏。
              
            class CAge:
                
            public CObject
            {   
                DECLARE_DYNAMIC(CAge)
            };
            IMPLEMENT_DYNAMIC(CAge,CObject)
            int main()
            {
                
            // This example creates an object if CMyClass is defined.
                CAge * pMyObject=new CAge;
                CRuntimeClass
            * pMyRTClass= pMyObject->GetRuntimeClass();
                 
                CRuntimeClass
            * pClass = pMyRTClass->FromName("CMyClass");
                
            if (pClass == NULL)
                {
                   
            // not found, display a warning for diagnostic purposes
                   AfxMessageBox("Warning: CMyClass not defined");
                   
            return NULL;
                }
                 
                
            // attempt to create the object with the found CRuntimeClass
                CObject* pObject = pClass->CreateObject();
                cout 
            << pObject->GetRuntimeClass()->m_lpszClassName <<endl;
                system(
            "pause");
                
            return 0;
            }




            代碼2:
            #include <iostream>
            #include <afxwin.h>
            using namespace std;
            class CMyClass:
                
            public CObject
            {   
                DECLARE_SERIAL(CMyClass)
            };
            IMPLEMENT_SERIAL(CMyClass,CObject,1)
              
            int main()
            {
                CObject* pObject = (RUNTIME_CLASS(CObject))->CreateObject("CMyClass");
                cout << pObject->GetRuntimeClass()->m_lpszClassName <<endl;
                system("pause");
                
            return 0;
            }



            //代碼3
            #include <iostream>
            #include 
            <afxwin.h>

            using namespace std;

            class CMyA:public CObject{
                DECLARE_DYNCREATE(CMyA);
            public:
                
            void show(){
                    cout 
            << "CMyA::show()" << this-><<endl;
                }
                
            int n;
            };
            IMPLEMENT_DYNCREATE(CMyA,CObject);


            int main(int argc, char* argv[]) {
                CObject
            * a = RUNTIME_CLASS(CMyA)->CreateObject();
                CMyA
            * b = (CMyA*)a;
                b
            ->= 5;
                b
            ->show();
                
            return 0;
            }

            //注意代碼1和代碼3之間的區(qū)別.

            posted on 2010-10-26 17:04 天下 閱讀(539) 評(píng)論(1)  編輯 收藏 引用

            評(píng)論

            # shi 2010-12-22 09:29 uk dress

            good post...I like this very much...  回復(fù)  更多評(píng)論   


            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


            <2012年10月>
            30123456
            78910111213
            14151617181920
            21222324252627
            28293031123
            45678910

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            留言簿(4)

            隨筆分類(378)

            隨筆檔案(329)

            鏈接

            最新隨筆

            搜索

            最新評(píng)論

            日韩精品久久久肉伦网站| 热99re久久国超精品首页| 77777亚洲午夜久久多喷| 亚洲精品国产综合久久一线| 亚洲人成电影网站久久| 久久亚洲国产成人精品性色| 18岁日韩内射颜射午夜久久成人| 亚洲精品久久久www| 精品999久久久久久中文字幕| 久久伊人影视| 91精品无码久久久久久五月天| 久久久午夜精品福利内容| 久久免费精品视频| 精品无码久久久久国产动漫3d| 日本免费久久久久久久网站| 久久国产色av免费看| 国产精品午夜久久| 精品久久久久久久久午夜福利| 久久青青草原精品国产不卡| 狠狠色噜噜狠狠狠狠狠色综合久久| 久久久久久免费视频| 99久久人人爽亚洲精品美女| 久久发布国产伦子伦精品 | 久久久久久国产a免费观看不卡 | 午夜精品久久久久久久无码| 狠狠色噜噜狠狠狠狠狠色综合久久| 亚洲国产一成人久久精品| 色综合久久夜色精品国产| 精品国产青草久久久久福利| 97热久久免费频精品99| 7777久久亚洲中文字幕| 99久久婷婷免费国产综合精品| 久久久久久人妻无码| 久久婷婷五月综合色奶水99啪| 精品国产乱码久久久久久人妻| 久久人妻无码中文字幕| 久久99久国产麻精品66| 久久久久久久波多野结衣高潮| 久久久高清免费视频| 色婷婷综合久久久久中文一区二区| 亚洲人成精品久久久久|