这里只是简单些个序列化的逻辑. 做个笔记而已.
字符串转换成结构体类似. 读者如果有兴趣可以采用 Json 的字符串格式.
-
#include <iostream>
-
#include <string>
-
#include <sstream>
-
using namespace std;
-
-
template<class T>
-
string to_string( T t )
-
{
-
ostringstream oss;
-
oss << t;
-
result = oss.str();
-
}
-
-
-
#define TransBegin( cls ) \
-
template<>\
-
string to_string ( cls t )\
-
{\
-
string strContent("{\n");\
-
-
#define TransMenber(value) \
-
{\
-
stringstream sio;\
-
sio << #value << " : " << t.value << "\n" ;\
-
strContent += sio.str();\
-
}\
-
-
#define TransEnd() \
-
strContent += "}";\
-
return strContent;\
-
}\
-
-
struct Data
-
{
-
int numberData;
-
string strData;
-
};
-
TransBegin(Data)
-
TransMenber(numberData)
-
TransMenber(strData)
-
TransEnd()
-
-
void main()
-
{
-
-
Data data;
-
string str = to_string(data);
-
-
cout << str << endl;
-
-
}
阅读(1023) | 评论(0) | 转发(0) |