首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

单件模式+property怎么使用

2012-05-24 
单件模式+property如何使用?大家好,我先上代码C/C++ code#include stdafx.h#include stringusing name

单件模式+property如何使用?
大家好,我先上代码

C/C++ code
#include "stdafx.h"#include <string>using namespace std;using namespace System;class TestDatas{public:    ~TestDatas(void){};    static TestDatas * InstanceDatas()    {        if (gDatas == NULL)        {            gDatas = new TestDatas();        }        return gDatas;    }    //get/set    string Get_myName()    {        return myName;    }    void Set_myName(string i_myName)    {        myName = i_myName;    }private:    TestDatas(void){};    static TestDatas * gDatas;    string myName;};TestDatas * TestDatas::gDatas = 0;int _tmain(int argc, _TCHAR* argv[]){    TestDatas * gdata = TestDatas::InstanceDatas();    gdata->Set_myName("tree");    string myName = gdata->Get_myName();    return 0;}


上面的代码中使用了单件模式,但是get/set是自己写的,于是我想,能不能使用property,可以免去写get/set。
C/C++ code
ref class TestClass{public:    property int myName;};

请大家支招,谢谢!


[解决办法]
可以,但是这样的话程序比较死,在初始化时赋值,以后再也不能赋值了,如果放在函数里,我们随时调用函数进行赋值,很灵活的。
[解决办法]
c#里的proprity就是取代get(),put()的

热点排行