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

替数据结构添加setProperty和getProperty

2012-09-12 
为数据结构添加setProperty和getProperty为手头游戏项目写编器,关联编器和游戏中的游戏对象的属性成了一个

为数据结构添加setProperty和getProperty

为手头游戏项目写编器,关联编器和游戏中的游戏对象的属性成了一个不小的问题,经过多经改写总算有一个相对好点的方案了,如果大家有更好的解决方案,请指点:

不废话,上代码:

原文地址:http://blog.csdn.net/wzq9706/article/details/7924881


myProperty.h

// 王智泉#include "myPropertySet.h"#include "myProperty.h"#include <iostream>class myTest : public myPropertySet{public:myTest();int _intVal;float _floatVal;std::string _strVal;bool _boolVal;};// 定义属性myDEF_PROPERTY(myTest, "IntValue", _intVal, "这是整型");myDEF_PROPERTY(myTest, "FloatValue", _floatVal, "这是浮点型");myDEF_PROPERTY(myTest, "StringValue", _strVal, "这是字符串");myDEF_PROPERTY(myTest, "BOOLValue", _boolVal, "这是布尔型");myTest::myTest(){// 注册属性,一定人先调用myDEF_PROPERTY定义对应的属性myRegisterProperty(myTest, _intVal);myRegisterProperty(myTest, _floatVal);myRegisterProperty(myTest, _strVal);myRegisterProperty(myTest, _boolVal);}int main(){myTest test;test.setProperty("IntValue", "10");test.setProperty("FloatValue", "10");test.setProperty("StringValue", "我叫王智泉,哈哈....");test.setProperty("BOOLValue", "TRUE");std::cout << "IntValue:" << test._intVal  << "\nFloatValue:" << test._floatVal << "\nStringValue:" << test._strVal << "\nBOOLValue:" << test._boolVal << std::endl;test._intVal = 100;test._floatVal = 100.0f;test._strVal = "Are you a programming monkey?";test._boolVal = false;std::cout << "\nIntValue:" << test.getProperty("IntValue")  << "\nFloatValue:" << test.getProperty("FloatValue") << "\nStringValue:" << test.getProperty("StringValue") << "\nBOOLValue:" << test.getProperty("BOOLValue") << std::endl;return 0;}

输出结果:

IntValue:10
FloatValue:10
StringValue:我叫王智泉,哈哈....
BOOLValue:1


IntValue:100
FloatValue:100.00
StringValue:Are you a programming monkey?
BOOLValue:FALSE

热点排行