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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:57:18+00:00 2026-05-13T23:57:18+00:00

I need start off with code because I am not sure what terminology to

  • 0

I need start off with code because I am not sure what terminology to use. Lets say I have the following code:

 class Node
 { 
 public:
  void Parse(rapidxml::xml_node<> *node) 
  {
   for (rapidxml::xml_attribute<> *attr = node->first_attribute();
        attr;
        attr = attr->next_attribute())
   {
    std::stringstream converter;
    converter << attr->value();

    if( !strcmp(attr->name(), "x") ) converter >> x;
    else if( !strcmp(attr->name(),"y") ) converter >> y;
    else if( !strcmp(attr->name(), "z") ) converter >> z;
   }
  }

 private:
  float x;
  float y;
  float z;
 };

What I can’t stand is the repetition of if( !strcmp(attr->name(), “x”) ) converter >> x; I feel that this is error prone and monotonous, but I cannot think of another way to map a string value to a member assignment. What are some other approaches one can take to avoid code such as this? The only other possible alternative I could think of was to use a hashmap, but that runs into problems with callbacks

This is the best I could up with but it’s not as flexible as I’d like:

 class Node
 {
  Node() : x(0.0f), y(0.0f), z(0.0f) 
  {
   assignmentMap["x"] = &x;
   assignmentMap["y"] = &y;
   assignmentMap["z"] = &z;
  }

 public:
  void Parse(rapidxml::xml_node<> *node) 
  {
   for (rapidxml::xml_attribute<> *attr = node->first_attribute();
        attr;
        attr = attr->next_attribute())
   {
    map<std::string, float*>::iterator member = assignmentMap.find(attr->name());
    //check for a pre-existing entry
    if( member == assignmentMap.end()) continue;

    std::stringstream converter;
    converter << attr->value();
    converter >> *(member->second);
   }
  }

 private:
  float x;
  float y;
  float z;

  std::map<std::string, float*> assignmentMap;
 };
  • 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-13T23:57:19+00:00Added an answer on May 13, 2026 at 11:57 pm

    For the implementation with a map, you could use pointers-to-members. Then you won’t need a deep copy of the map (when you copy it, the pointers in the map still point into the original Node), and it will also allow you to make the whole thing static (this map is unnecessary at per-instance basis).

    For example:

    class Node {
        //...
        static std::map<std::string, float Node::*> initVarMap();
        static float Node::* varFromName(const std::string& name);
    };
    
    std::map<std::string, float Node::*> Node::initVarMap()
    {
        std::map<std::string, float Node::*> varMap;
        varMap["x"] = &Node::x;
        varMap["y"] = &Node::y;
        varMap["z"] = &Node::z;
        return varMap;
    }
    
    float Node::* Node::varFromName(const std::string& name)
    {
        static std::map<std::string, float Node::*> varMap = initVarMap();
        std::map<std::string, float Node::*>::const_iterator it = varMap.find(name);
        return it != varMap.end() ? it->second : NULL;
    }
    

    Usage:

        float Node::* member(varFromName(s));
        if (member)
            this->*member = xyz;
    

    This isn’t any more flexible, though.

    To support different types of members, you might modify the above to use a map of string to “variant of all supported member types”.

    For example so. The member setter visitor should be reusable, and the only change to the code, to add or change member types, should be done to the typedef.

     #include <map>
     #include <string>
     #include <iostream>
     #include <boost/variant.hpp>
    
    template <class Obj, class T>
    struct MemberSetter: boost::static_visitor<void>
    {
        Obj* obj;
        const T* value;
    public:
        MemberSetter(Obj* obj, const T* value): obj(obj), value(value) {}
    
        void operator()(T Obj::*member) const
        {
            obj->*member = *value;
        }
        template <class U>
        void operator()(U Obj::*) const
        {
            //type mismatch: handle error (or attempt conversion?)
        }
    };
    
    class Node
    {
    public:
        Node() : i(0), f(0.0f), d(0.0f)
        {
        }
    
        template <class T>
        void set(const std::string& s, T value)
        {
            std::map<std::string, MemberTypes>::const_iterator it = varMap.find(s);
            if (it != varMap.end()) {
                boost::apply_visitor(MemberSetter<Node, T>(this, &value), it->second);
            } //else handle error
        }
        void report() const
        {
            std::cout << i << ' ' << f << ' ' << d << '\n';
        }
    private:
        int i;
        float f;
        double d;
    
        typedef boost::variant<int Node::*, float Node::*, double Node::*> MemberTypes;
        static std::map<std::string, MemberTypes> initVarMap();
        static std::map<std::string, MemberTypes> varMap;
    };
    
    int main()
    {
        Node a;
        a.set("i", 3);
        a.set("d", 4.5);
        a.set("f", 1.5f);
        a.report();
    }
    
    std::map<std::string, Node::MemberTypes> Node::initVarMap()
    {
        std::map<std::string, Node::MemberTypes> varMap;
        varMap["i"] = &Node::i;
        varMap["f"] = &Node::f;
        varMap["d"] = &Node::d;
        return varMap;
    }
    
    std::map<std::string, Node::MemberTypes> Node::varMap = Node::initVarMap();
    

    This is naturally just an example of what you can do. You can write a static_visitor to do what you want. E.g storing a stream and attempting to extract a value of the right type for the given member.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Need to set the TabsScroll property to true on the… May 14, 2026 at 7:03 am
  • Editorial Team
    Editorial Team added an answer You may want to use the following query. SELECT CONCAT('You… May 14, 2026 at 7:03 am
  • Editorial Team
    Editorial Team added an answer Check out this Scott Hanselman article: Plug-In Hybrids: ASP.NET WebForms… May 14, 2026 at 7:03 am

Related Questions

Let me start off by saying that I am very new to WPF and
I read through quite a few similar questions here on SO but none were
Can anyone point me to a program that strips off strings from C source
So, I'm starting to write some logic for a simple program (toy game on
I have a table of about a million rows and I need to update

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.