我可以通过一个C ++类的(公共)属性循环? [英] Can I loop through (public) attributes of a C++ class?

查看:111
本文介绍了我可以通过一个C ++类的(公共)属性循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现这两个C的结构和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)。我想通过UDP这些属性发送到Java客户端。我的想法是创建一个字节(字符*)与三个属性缓冲(我照顾的字节顺序问题)。

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 ++类的(公共)属性循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆