-7

So If I have a struct as follows:

struct MyObject {
    int x;
    int y;
};

Is there any way to get or manipulate MyObject's data members WITHOUT knowing their name?

In other words, achieving the same thing as:

MyObject.x = 10;

, but given that I don't know that MyObject has data member named x..

So something like this:

MyObject[0] = 10;  // x = MyObject's first data member....
MyObject[1] = 20;  // y = MyObject's second data member....
user2436815
  • 3,455
  • 5
  • 27
  • 40

2 Answers2

0

You can use an anonymous union

Union u {
    Myobject obj;
    Int a[2];
};

// U.a[0] corresponds to u.obj.x
// U.a[1] corresponds to u.obj.y
// 

Note that i believe obj must be POD for this to work.

tohava
  • 5,344
  • 1
  • 25
  • 47
0
void func(void* data)
{
    int* a = (int*) data;
    *a++ = 10;
    *a++ = 20;
}

would change an object. But IMHO you are way lost studing "instances vs classes"

Exceptyon
  • 1,584
  • 16
  • 23