Can I loop through (public) attributes of a C++ class?(我可以遍历 C++ 类的(公共)属性吗?)
问题描述
我找到了 C Structs 和 C# 类的答案,但对 C++ 却空手而归.在 C 中,你不能.在 C# 中,它是 GetProperties() 方法.
I found the answer for both C Structs and C# classes but came back empty-handed concerning C++. EDIT : In C, you can't. In C# it's the GetProperties() method.
上下文:我有一个具有公共属性的 C++ 类(比如说一个带有 X、Y、Z 的 Point).我想通过 UDP 将这些属性发送到 Java 客户端.我的想法是创建一个具有三个属性的字节(char *)缓冲区(我处理了字节序问题).
The context: I have a C++ class with public attributes (let's say a Point with X, Y, Z). I want to send these attributes via UDP to a Java client. My idea was to create a byte (char *) buffer with the three attributes (i took care of the endianness problems).
prepareForUdp(char * buffer)
{
int offset = 0;
int offsetValue = 4;
char tempBuffer[16];
memcpy( tempBuffer, &X_, sizeof(X_) );
offset = offset + offsetValue;
memcpy( tempBuffer + offset, &Y_, sizeof(Y_) );
offset = offset + offsetValue;
memcpy( tempBuffer + offset, &Z_, sizeof(Z_) );
offset = offset + offsetValue;
memcpy( buffer, tempBuffer, sizeof(buffer) );
}
我希望我的界面是进化的,因为该点可能会获得第四、五或第 n 维,并且我希望我的 prepareForUdp() 方法是(相对)通用的.
I want my interface to be evolutive, because the point may get a fourth, a five, or an n-th dimension, and I want my prepareForUdp() method to be (relatively) generic.
我的问题是:如何循环(或迭代)我的属性?
My question is : how do I loop (or iterate) through my attributes ?
推荐答案
C++中没有反射.所以,答案是,你不能:)
There is no reflection in C++. So, the answer is, you can't :)
这篇关于我可以遍历 C++ 类的(公共)属性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:我可以遍历 C++ 类的(公共)属性吗?
基础教程推荐
- C++ #define 1970-01-01
- \a序列表示“钟”字符。 1970-01-01
- 读取名字和身高并显示信息 1970-01-01
- C++函数调用运算符()重载 1970-01-01
- C++ - 指向对象的向量的性能与对象的性能 2022-10-18
- 使用printf()显示int类型的变量 1970-01-01
- C语言库difftime()函数 1970-01-01
- C++递增指针 1970-01-01
- 使用基本数据类型,并输出它们的值,int,floa 1970-01-01
- C++条件编译 1970-01-01