Bind members of different classes

Posted by 7vies on Stack Overflow See other posts from Stack Overflow or by 7vies
Published on 2011-01-02T01:47:53Z Indexed on 2011/01/02 1:54 UTC
Read the original article Hit count: 452

Filed under:
|

In a C++ program I have two classes (structs) like

struct A
{
    int x;
    double y;
    // other members
};

struct B
{
    int x;
    double y2;
    // other members
};

I'd like to somehow "bind" the corresponding members, e.g. A::x to B::x and A::y to B::y2. By "bind" I mean ability to obtain a reference to the bound variable, for example given a member of class A I could assign it to the value of the corresponding B member.

Once I have such bind, I'd like to build a bind table or something similar which I could iterate over. This would allow, for example, copying the corresponding fields from A a; to B b; like CopyBound(a, b, bind_table);, but probably also doing some other things not limited to Copy interface.

The problem with this bind_table is that I want static typing and the bind_table would have to contain different types in this case. For example, a table of pointers to class members would contain &A::x and &A::y, but they are of different type, so I cannot just put them say into an array.

Any ideas how this can be conveniently implemented, having as much compile-time type checking as possible?

© Stack Overflow or respective owner

Related posts about c++

Related posts about c