Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3661344
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:17:42+00:00 2026-05-19T01:17:42+00:00

In a C++ program I have two classes (structs) like struct A { int

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T01:17:43+00:00Added an answer on May 19, 2026 at 1:17 am

    Perhaps you want some template metaprogramming along the lines of this:

    #include <iostream>
    
    // An item in the list of bound members
    template<typename Class, typename T, T Class::*Ptr, typename Next = void> struct m
    {
        typedef Class containing_type;
        typedef T value_type;
        typedef Next next;
        static T Class::*pointer;
    };
    template<typename Class, typename T, T Class::*Ptr, typename Next>
        T Class::* m<Class, T, Ptr, Next>::pointer = Ptr;
    
    // Iterator for the member list
    template<typename Item1, typename Item2> struct apply_helper
    {
        typedef typename Item1::containing_type A;
        typedef typename Item2::containing_type B;
    
        template<typename Op>
        static void apply(A& a, B& b, Op op)
        {
            op(a.*Item1::pointer, b.*Item2::pointer);
            apply_helper<typename Item1::next, typename Item2::next>::apply(a, b, op);
        }
    };
    
    template<typename Item1> struct apply_helper<Item1, void>
    {
        // An error referencing this means you forgot a member in the list
    };
    template<typename Item2> struct apply_helper<void, Item2>
    {
        // An error referencing this means you forgot a member in the list
    };
    template<> struct apply_helper<void, void>
    {
            template<typename A, typename B, typename Op>
            static void apply(A& a, B& b, Op op) {}
    };
    
    
    // This function initiates the enumeration of the members
    template<typename A, typename B, typename Op> void apply(A& a, B& b, Op op)
    {
        apply_helper<typename A::members, typename B::members>::apply(a, b, op);
    }
    
    
    // EXAMPLE
    
    struct MyA
    {
        int v1;
        double v2;
        typedef m<MyA, int, &MyA::v1,
                m<MyA, double, &MyA::v2> > members;
    };
    
    struct MyB
    {
        int v1;
        double v2;
        typedef m<MyB, int, &MyB::v1,
                m<MyB, double, &MyB::v2> > members;
    };
    
    // A functor which demonstrates printing the value before copying    
    struct CopyAnyValue
    {
        template<typename T> void operator()(T& t1, T& t2)
        {
            std::cout << "t1 = " << t1 << std::endl;
            t2 = t1;
        }
    };
    
    int main()
    {
        MyA a;
        MyB b;
    
        a.v1 = 3;
        a.v2 = 5.5;
    
        apply(a, b, CopyAnyValue());
    
        std::cout << "v1: " << b.v1 << "  v2: " << b.v2 << std::endl;
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an MFC C++ program that contains two classes, as follows; struct MyStruct
I have a program with two TForm classes and have added a TMainMenu to
In my program I have two classes: Collector and Entity. Collector stores Entities in
I have a strange Hibernate behaviour in my program. I have two classes with
I have two simple while loops in my program that I feel ought to
Is tight looping in a program bad? I have an application that has two
I would like a batch file to launch two separate programs then have the
In my program I have one array with 25 double values 0.04 When I
I have two classes, shown below: [Serializable] [XmlInclude(typeof(SomeDerived))] public class SomeBase { public string
I have two classes: Action class, that has a method for executing VBScript files,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.