accessing class properties with string
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Thread: accessing class properties with string

  1. #1
    Join Date
    Jun 2009
    Posts
    4

    accessing class properties with string

    I was wondering if there is a way to access a property of a class using a string.

    In javascript you can do this.
    test.x=0
    which is the same as
    test["x"]=0
    and
    var n="x";
    test[n]=0

    I honestly doubt this is possible. It would just make a situation easier where a function receives a property and value is passed as parameters.

  2. #2
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: accessing class properties with string

    test.x=0 works in C++.
    Thanks for your help.

  3. #3
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,725

    Re: accessing class properties with string

    A combination of std::map and boost::any may work.

    Code:
    std::map<std::string, boost::any> properties;
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  4. #4
    Join Date
    Jun 2009
    Posts
    4

    Re: accessing class properties with string

    Peter_APIIT
    I know test.x works in c++
    It sounds like I had not made it clear enough. That I wanted to access property x using a string.

    JohnW@Wessex
    I am not really sure how to use std::map
    It sounds like it is an associative array and that it can't be used to accessing an existing class. With my current playing an associative array might work well. Though I may have functions ect. Maybe I will have to bite the bullet and just code setX(property,value), getX(property,value) methods for each property. Would have been nice to have SetDouble(property,value) that could be used.

  5. #5
    Join Date
    Aug 2007
    Posts
    858

    Re: accessing class properties with string

    What exactly is your goal in trying to access class members with a string? It isn't totally clear (at least to me), but I get the feeling that you're trying to shoehorn C++ into doing something that you're used to doing in Javascript. If you can elaborate on what you want to do, then we can probably offer a solution more appropriate to C++.

  6. #6
    Join Date
    Jun 2009
    Posts
    1

    Re: accessing class properties with string

    [QUOTE=JohnW@Wessex;1847825]A combination of std::map and boost::any may work.

    ....stuff...[QUOTE]


    Is there a reason to use that library ? Except the library what are you probably introducing to peoople ?

  7. #7
    Join Date
    Aug 2008
    Posts
    112

    Re: accessing class properties with string

    Quote Originally Posted by JohnW@Wessex View Post
    A combination of std::map ..

    There is no reason to use that library, it is needless actually in this problem. I agree with those thinking you are trying to advertise something else in that site ? Did you ever get in debt and pay it yet ?
    hi,,,

  8. #8
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,725

    Re: accessing class properties with string

    Quote Originally Posted by Khiem View Post
    There is no reason to use that library, it is needless actually in this problem.
    I didn't say it was the ideal solution, just a way of doing it if the OP was sure it was really required.
    I agree with those thinking you are trying to advertise something else in that site ?
    Who said that? No one!
    Boost is an free, open source library, the CPP Reference website is a useful resource for STL information.
    What exactly are you implying that I am trying to advertise?
    Did you ever get in debt and pay it yet ?
    I have no idea what that is supposed to imply

    EDIT: AdvertiseYourFamilyMember's post suddenly popped up 10 minutes after your post. I assume you're talking about his comment?
    Except the library what are you probably introducing to peoople ?
    Have you not heard of Boost?
    Last edited by JohnW@Wessex; June 3rd, 2009 at 06:55 AM.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  9. #9
    Join Date
    Aug 2008
    Posts
    112

    Re: accessing class properties with string

    I suggest not using that library to resolve this problem as VB/C# with ASP or PHP already helps
    It is much easier and less less less least confusing!
    I imply I am a human not an animal, so I am emotional about that.
    hi,,,

  10. #10
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,891

    Re: accessing class properties with string

    We've had some strange newbies lately....

  11. #11
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,345

    Re: accessing class properties with string

    Quote Originally Posted by Khiem
    I suggest not using that library to resolve this problem as VB/C# with ASP or PHP already helps
    It is much easier and less less less least confusing!
    It may indeed be the case that another programming language is more suitable than C++ for the job at hand, but greenie__ has not stated the job at hand. All greenie__ has stated is a desire to have class/object properties be accessible via a particular syntax. Saying that another programming language is more suitable is thus jumping to a conclusion.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  12. #12
    Join Date
    Jun 2009
    Posts
    4

    Re: accessing class properties with string

    I know it may seem like i'm trying to shoehorn c++ to do javascript(as speedo put it). I usually would just make classes as normal. This isn't my first c++ app, though what I do is just fiddle.
    Though I will explain. I have been fiddling with v8 engine. it is pretty nifty.
    You can attach callback function to javascript object properties.
    What it calls is myCallbackFunction(property,Value).

    I was just curious if it was possible. Would have made exposing c++ classes a lot easier.

    I figured it would not be possible to do this. Though I just had to ask.

    Thanx For your help.

  13. #13
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,637

    Re: accessing class properties with string

    You can come close by doing something like the code below
    (but I son;t really like the looks of it). One problem is operator []
    can only work with one type (I don't know if boost:any would
    help or not). The "Set" option below could be made to work
    with multiple types.

    Code:
    #include <iostream>
    #include <map>
    #include <string>
    #include <stdexcept>
    
    using namespace std;
    
    class S1
    {
    public:
    
        S1()
        {
            int_map["i1"] = &i1;
            int_map["i2"] = &i2;
        }
    
    
        int & operator[] (const std::string & s)
        {
            map<string,int*>::const_iterator it = int_map.find(s);
    
            if (it != int_map.end())
            {
                return *it->second;
            }
            else
            {
                throw std::invalid_argument(s.c_str());
                // might need to return something here on some compilers
            }
        }
    
        const int & operator[] (const std::string & s) const
        {
            map<string,int*>::const_iterator it = int_map.find(s);
    
            if (it != int_map.end())
            {
                return *it->second;
            }
            else
            {
                throw std::invalid_argument(s.c_str());
                // might need to return something here on some compilers
            }
        }
    
    private:
    
        int i1;
        int i2;
    
        map<string,int*> int_map;
    };
    
    
    
    template <typename Tobj , typename T>
    void Set(Tobj & obj, const std::string & s , const T & value)
    {
        obj[s] = value;
    }
    
    
    int main()
    {
        S1 s1 , s2;
    
        try
        {
            s1["i1"] = 1;
            s1["i2"] = 2;
    
            s2["i1"] = 3;
            s2["i2"] = 4;
    
            cout << s1["i1"] << " " << s1["i2"] << " " << s2["i1"] << " " << s2["i2"] << "\n";
    
    
            Set(s1,"i1",3);
            Set(s1,"i2",6);
            Set(s2,"i1",4);
            Set(s2,"i2",8);
    
            cout << s1["i1"] << " " << s1["i2"] << " " << s2["i1"] << " " << s2["i2"] << "\n";
    
            s1["something unexpected"] = 5;
        }
        catch (std::invalid_argument & e)
        {
            cout << e.what() << "\n";
        }
    
        return 0;
    }

  14. #14
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: accessing class properties with string

    Quote Originally Posted by Lindley View Post
    We've had some strange newbies lately....
    I agree.

    I was going to propose something like what Philip mentioned when I saw this from the OP:
    Quote Originally Posted by greenie__
    Would have been nice to have SetDouble(property,value) that could be used.
    This hints to having a template based on the type of value being set.

    If you provide more detail on how you get that property string, there might be more ideas to implementing it. For example, I am guessing that there is a very small chance that you might be needing some kind of serialization/deserialization. Is that it?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center