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

  • SEARCH
  • Home
  • 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 6998629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:26:08+00:00 2026-05-27T20:26:08+00:00

I am working on a piece of code that copies a ‘Person’ object from

  • 0

I am working on a piece of code that copies a ‘Person’ object from one data representation to another. The names in each class (name, address, title) match, and all types are strings. For each field I want to apply the same transformation, based on some condition that also depends on the field name. The tricky part is that the repeating code uses function suffixes that are based on the field name. It looks something like this:

LibraryA::Person person1;
LibraryB::Person person2;

if (person1.name_valid() && [...somestuff...]) {
    string *v = SomeOtherFunction(person.name())
    person2.set_name(v);
}
if (person1.address_valid() && [...somestuff...]) {
    string *v = SomeOtherFunction(person.address())
    person2.set_address(v);
}
if (person1.title_valid() && [...somestuff...]) {
    string *v = SomeOtherFunction(person.title())
    person2.set_title(v);
}

Is there a trick (or technique 🙂 ) to factor out the repetitive part to a template? I’d prefer a solution that does not involve defining a macro (that would be too easy 🙂 )

  • 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-27T20:26:08+00:00Added an answer on May 27, 2026 at 8:26 pm

    This fits your requirements, but whether I would use it or not is a different question. Only if there is a huge amount of repetition I would go through this path, and then I would combine it with a macro to simplify the calling code:

    void test_and_set( Person const & person1, Person & person2, 
                       bool (Person::*test)() const,
                       std::string (Person::*get)() const,
                       void (Person::*set)( std::string const &) )
    {
       if ( (person1.*test)() ) {
          (person2.*set)( (person1.*get)() );
       }
    }
    

    Used as:

    test_and_set( person1, person2, &Person::valid_name, &Person::get_name, &Person::set_name );
    

    And combined with a local macro:

    #define TEST_AND_SET( p1, p2, field ) \
        test_and_set( (p1), (p2), &Person::valid_##field, &Person::get_##field, &Person::set_##field )
    
    TEST_AND_SET( person1, person2, name );
    TEST_AND_SET( person1, person2, title );
    #undef TEST_AND_SET
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a piece of code that writes data from 4 different
I'm working from an example piece of code that allocates a relatively large local
I'm working on a piece of code that I inherited and am trying to
I'm working on a piece of code that GETs a URL and parses the
I have a working piece of code in the application delegate that contains a
I am currently working on the maintenance of a piece of code that is
I am currently working with a piece of code that has been written by
I've been working on a piece of code that that pulls the name of
I've been working on this small piece of code that seems trivial but still,
I am trying to write a piece of code that searches one column of

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.