Jump to content
  • Advertisement
Sign in to follow this  
gameplayprogammer

Alternative to #define VARNAME "text".....

This topic is 3621 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

(in VS2005/C++/std) Alternative to #define VARNAME "text"..... i am reading in some strings and mapping them onto unique ids. example: #define BOX1 "box1" #define BOX2 "box2" i want to use an alternative to #define.... i am thinking writing a function that passes in a string, compares this with a list of const strings and returns the integer id. std::map<std::string,int> stringList; int FindID(std::string& findThis) { return ( stringList[findThis] ); } Anyone know any simpler/better ideas or something i am missing?

Share this post


Link to post
Share on other sites
Advertisement
I don't really know what your problem is, but in C++ we try to use const variables instead of #defines.

const std::string BOX1 = "box1";
const std::string BOX2 = "box2";


Post a bit more information on what you're trying to do.

Share this post


Link to post
Share on other sites
What you posted;


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

int FindID(std::string& findThis)
{
return ( stringList[findThis] );
}



won't work as you expect.

If the string 'findThis' doesn't exist then a new entry will be created and a default constructed int will be returned.

What you should do is use std::map::find() and check the returned iterator to make sure the entry exists and then retrive it (via deferencing the iterator to get at the value).

Share this post


Link to post
Share on other sites
I want to link the value of a const integer to the value of a string.
so where ever i use the string it substitutes it for the value of the const integer.

const int objectOneId = 5;

std::string string = ReadStringFromFile();

// This is what i want help with...
// the string is checked against a std::map<std::string,int> stringList;
int id = GetIDFromString(string);
someObject->SetID(id);


if(someObject->GetID() == objectOneId)
{
// this is object one
}

Share this post


Link to post
Share on other sites
If I understand what you want, you should use a pointer. Technically, it's an integer that can be set to constant that can be substituted for the value of a string.


string x = "qwerty";

const string* xPtr = &x; // xPtr equals the address/value of x.
// The & prefixed to a variable is the address extractor.

if( *xPtr == "qwerty" ) // If the pointer points to a value equal to qwerty...
// The *prefixed to a pointer de-references, or allows you to see what the pointer points to.




This should in no way be a substitute for a tutorial on pointers if you really don't know what they are. They can be quite a hassle to work with, but once you understand them, the list of things you can do shoots up exponentially.

Share this post


Link to post
Share on other sites
Technically, it is a whole number, or integer that represents a place in memory. Might be good to clarify positive whole number, or unsigned integer.

But if you disagree, I'd be happy to read WHY? (Wikipedia, by the way, has no entry for "whole number", it redirects you to integer.)

Share this post


Link to post
Share on other sites
Quote:
Original post by Splinter of Chaos
If I understand what you want, you should use a pointer. Technically, it's an integer that can be set to constant that can be substituted for the value of a string.

Ah, where's Zahlman when you need him to explain the deep doctrine of the difference between integers and pointers? C++ makes far fewer guarantees about pointers than integers (e.g. theint++ is always defined while theptr++ may not be defined, as after the end of an array).

Share this post


Link to post
Share on other sites
I was (a) using the word integer because s/he asked specifically about using an integer that works as the specific string and (b), either way, trying to suggest s/he use pointers. (Or at least learn to use them.)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!