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 169857
In Process

The Archive Base Latest Questions

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

Let’s suppose I have a struct like this: struct my_struct { int a; int

  • 0

Let’s suppose I have a struct like this:

struct my_struct {   int a;   int b;  } 

I have a function which should set a new value for either ‘a’ or ‘b’. This function also requires to specify which variable to set. A typical example would be like this:

void f(int which, my_struct* s, int new_value) {   if(which == 0)      s->a = new_value;   else      s->b = new_value;  } 

For reasons I won’t write here I cannot pass the pointer to a/b to f. So I cannot call f with address of my_struct::a or my_struct::b. Another thing I cannot do is to declare a vector (int vars[2]) within my_struct and pass an integer as index to f. Basically in f I need to access the variables by name.

Problem with previous example is that in the future I plan to add more variables to struct and in that case I shall remember to add more if statements to f, which is bad for portability. A thing I could do is write f as a macro, like this:

#define FUNC(which) void f(my_struct* s, int new_value) \ { \         s->which = new_value; \ }  

and then I could call FUNC(a) or FUNC(b).

This would work but I don’t like using macros. So my question is: Is there a way to achieve the same goal using templates instead of macros?

EDIT: I’ll try to explain why I cannot use pointers and I need access to variable by name. Basically the structure contains the state of a system. This systems needs to ‘undo’ its state when requested. Undo is handled using an interface called undo_token like this:

class undo_token { public:    void undo(my_struct* s) = 0; }; 

So I cannot pass pointers to the undo method because of polymorphism (mystruct contains variables of other types as well).

When I add a new variable to the structure I generally also add a new class, like this:

class undo_a : public undo_token {   int new_value; public:   undo_a(int new_value) { this->new_value = new_value; }   void undo(my_struct *s) { s->a = new_value} }; 

Problem is I don’t know pointer to s when I create the token, so I cannot save a pointer to s::a in the constructor (which would have solved the problem). The class for ‘b’ is the same, just I have to write ‘s->b’ instead of s->a

Maybe this is a design problem: I need an undo token per variable type, not one per variable…

  • 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. 2026-05-11T12:42:20+00:00Added an answer on May 11, 2026 at 12:42 pm
    #include <iostream> #include <ostream> #include <string>  struct my_struct {     int a;     std::string b; };  template <typename TObject, typename TMember, typename TValue> void set( TObject* object, TMember member, TValue value ) {     ( *object ).*member = value; }  class undo_token {};  template <class TValue> class undo_member : public undo_token {     TValue new_value_;     typedef TValue my_struct::* TMember;     TMember member_;  public:     undo_member(TMember member, TValue new_value):         new_value_( new_value ),         member_( member )     {}      void undo(my_struct *s)      {          set( s, member_, new_value_ );     } };      int main() {     my_struct s;      set( &s, &my_struct::a, 2 );     set( &s, &my_struct::b, 'hello' );      std::cout << 's.a = ' << s.a << std::endl;     std::cout << 's.b = ' << s.b << std::endl;      undo_member<int> um1( &my_struct::a, 4 );     um1.undo( &s );      std::cout << 's.a = ' << s.a << std::endl;      undo_member<std::string> um2( &my_struct::b, 'goodbye' );     um2.undo( &s );      std::cout << 's.b = ' << s.b << std::endl;      return 0; } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 79k
  • Answers 79k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There are about a hundred terminology issues here, mostly built… May 11, 2026 at 4:12 pm
  • Editorial Team
    Editorial Team added an answer My favorite tool is Xenu. May 11, 2026 at 4:12 pm
  • Editorial Team
    Editorial Team added an answer I don't know that I understand your question. You can… May 11, 2026 at 4:12 pm

Related Questions

Let's say you create a wizard in an HTML form. One button goes back,
Let's say I'm building a data access layer for an application. Typically I have
Let's say you have a class called Customer, which contains the following fields: UserName
Let me try to explain what I need. I have a server that is
Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.