Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

bilsa

string to variable ?

This topic is 5432 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
Advertisement
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]

Share this post


Link to post
Share on other sites
I think what you''re looking for is a map.


#include <map>
#include <string>

std::map<std::string,int> myVars;

myVars[ "SomeVariableName" ] = 12;


Am I correct?

-Odd the Hermit

Share this post


Link to post
Share on other sites
In combination with a map :


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 ]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!