string to variable ?
Heh, I posted a message on the newbee forum about linked lists... but that post got totally messed up by me, so I'll give it another shot here.
So after a couple of hours thinking about my problem my conclution is that what I need to do is something like this:
Object
{
int nTestVariable;
}
char* sVariableName = "nTestVariable"
object.sVariableName = 10;
If i print object.nTestVariable, I want to get 10 as the result
That is the effect I would want to achieve.
The important this is that I need to be able to do the
//object.(still not choosen variable)
object.sVariableName = 10;
//object2.(still not choosen variable)
object2.sVariableName = 20;
thing.
Would be greatful for any help !
[edited by - bilsa on September 3, 2003 3:32:59 PM]
C++ has no introspection mechanism - you can't address variables 'by name'.
What language are you using ?
One option : pointers and member pointers.
int (Object::*sVariableName) = &Object::nTestVariable;
Object object;
object.*sVariableName = 10;
[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
[edited by - Fruny on September 3, 2003 3:42:33 PM]
What language are you using ?
One option : pointers and member pointers.
int (Object::*sVariableName) = &Object::nTestVariable;
Object object;
object.*sVariableName = 10;
[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
[edited by - Fruny on September 3, 2003 3:42:33 PM]
I think what you''re looking for is a map.
Am I correct?
-Odd the Hermit
#include <map>#include <string>std::map<std::string,int> myVars;myVars[ "SomeVariableName" ] = 12;
Am I correct?
-Odd the Hermit
In combination with a map :
Note - the type of the member and the type of the pointer must match.
[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
map<string, int (Object::*)> ObjectMemberMap;ObjectMemberMap["nTestVariable"] = &Object::nTestVariable;object.*ObjectMemberMap["nTestVariable"] = 10;
Note - the type of the member and the type of the pointer must match.
[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
Yeh guys!
Thats what I'm looking for!
Thank you greatly!
(the combination of member pointer and map)
and combined with a template that would rock!
(though... what i actually needed was the "&Object::nTestVariable")
[edited by - bilsa on September 3, 2003 3:51:35 PM]
[edited by - bilsa on September 3, 2003 4:00:36 PM]
Thats what I'm looking for!
Thank you greatly!
(the combination of member pointer and map)
and combined with a template that would rock!
(though... what i actually needed was the "&Object::nTestVariable")
[edited by - bilsa on September 3, 2003 3:51:35 PM]
[edited by - bilsa on September 3, 2003 4:00:36 PM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement