-
June 1st, 2009, 06:32 PM
#1
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.
-
June 2nd, 2009, 03:48 AM
#2
Re: accessing class properties with string
Thanks for your help.
-
June 2nd, 2009, 09:01 AM
#3
Re: accessing class properties with string
A combination of std::map and boost::any may work.
"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
-
June 2nd, 2009, 07:23 PM
#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.
-
June 2nd, 2009, 07:31 PM
#5
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++.
-
June 3rd, 2009, 04:23 AM
#6
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 ?
-
June 3rd, 2009, 04:45 AM
#7
Re: accessing class properties with string
Originally Posted by JohnW@Wessex
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,,,
-
June 3rd, 2009, 05:17 AM
#8
Re: accessing class properties with string
Originally Posted by Khiem
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 05: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
-
June 3rd, 2009, 05:56 AM
#9
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,,,
-
June 3rd, 2009, 09:30 AM
#10
Re: accessing class properties with string
We've had some strange newbies lately....
-
June 3rd, 2009, 09:30 AM
#11
Re: accessing class properties with string
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.
-
June 3rd, 2009, 10:16 PM
#12
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.
-
June 4th, 2009, 06:31 AM
#13
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;
}
-
June 7th, 2009, 08:50 AM
#14
Re: accessing class properties with string
Originally Posted by Lindley
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:
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?
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|