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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:15:23+00:00 2026-06-11T19:15:23+00:00

This question is a continuation of an earlier one found at: How to template'ize

  • 0

This question is a continuation of an earlier one found at: How to template'ize variable NAMES, not types?

Let’s say one has the following code:

struct VAR_TYPE{
public:
    bool is_fixed; 
    double value;      // Numerical value
    std::string name;  // Description of variable (to identify it by name)
    int reference_counter;
    /* ect. */
};

struct NODE{
public:
    VAR_TYPE X, Y, Z;
    /* ect. */
};

class MyClass{
public:
    std::vector <NODE_ptr> node;  // shared ptr, using boost::shared_ptr

    // what I have now
    void set_variable_X(int &i, double &P) { node[i]->X.value = P; }
    void set_variable_Y(int &i, double &P) { node[i]->Y.value = P; } 
    void set_variable_Z(int &i, double &P) { node[i]->Z.value = P; }

    // What I want to replace all 3 members with:
    <class T, class N>
    void set_variable( int &i, double &P, /* ??? */ ) { /* ??? */ }
    /* ect. */
};

I’m not sure what would go in areas where ‘???’ is written. Borrowing pseudocode used in the aforementioned link, I would like to something to the affect of

main(){
    MyClass S;
    double x1, y1, z1;
    int index; 

    // set x1, y1, z1, index

    S.set_variable( index, x1, &MyClass::node::X ); // in essence, what I want
    S.set_variable( index, y1, &MyClass::node::Y );
    S.set_variable( index, z1, &MyClass::node::Z );
};

I’ve tried a few ideas, but errors run a muck. I think the issues lies with the fact I am using a boost shared pointer and/or std::vector. Anyone have an idea what the problem, and a suitable solution, are? One options I have been working with is (but it does not use the calling convention I identified in the int main() above):

template < class T, class N >
void MyClass::set_reference( int &i, double &P,  
                         T NODE::* MemPtr,
                         N VAR_TYPE::* ValPtr)
{
    *MemPtr[i].*ValPtr.value = P; // doesn't work work
};
  • 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-06-11T19:15:24+00:00Added an answer on June 11, 2026 at 7:15 pm

    The following does what you seem to want:

    #include <string>
    #include <vector>
    struct VAR_TYPE{
    public:
        bool is_fixed; 
        double value;      // Numerical value
        std::string name;  // Description of variable (to identify it by name)
        int reference_counter;
        /* ect. */
    };
    
    struct NODE{
    public:
        VAR_TYPE X, Y, Z;
        /* ect. */
    };
    
    class MyClass{
    public:
        std::vector <NODE *> node;  // shared ptr, using boost::shared_ptr
    
        // what I have now
        void set_variable_X(int &i, double &P) { node[i]->X.value = P; }
        void set_variable_Y(int &i, double &P) { node[i]->Y.value = P; } 
        void set_variable_Z(int &i, double &P) { node[i]->Z.value = P; }
    
        // What I want to replace all 3 members with:
        void set_variable( int &i, double &P, VAR_TYPE NODE::* ptr ) { (node[i]->*ptr).value = P;}
        /* ect. */
    };
    
    
    main(){
        MyClass S;
        double x1, y1, z1;
        int index; 
    
        // set x1, y1, z1, index
    
        S.set_variable( index, x1, &NODE::X ); // in essence, what I want
        S.set_variable( index, y1, &NODE::Y );
        S.set_variable( index, z1, &NODE::Z );
    }
    

    Though I don’t understand what you mean by “A template to call variables by name”. BTW there’s no reason whatsoever to pass i and P by reference and you shouldn’t do it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok - this is in continuation from my earlier question about sending an email
This is in continuation of my earlier question where I wanted to compile many
This question is in continuation of this one . The deal is simple. Given:
This question is a continuation of my last one, regarding How to make Ruby
This is a continuation of a question I posed earlier . Currently when I
This question is a continuation of this one but asking for a more specific
This Question is a continuation to the one I posted before at: Excel -
This question is somehow a continuation of this one . I've been able to
This is a continuation to this question which Oleg has answered. I have 2
This could be considered a continuation of this earlier SO question . Ideally, I'd

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.