Sign in to follow this  
  • entries
    686
  • comments
    1952
  • views
    386227

Stuff with globals and templates and throw in some

Sign in to follow this  
Stompy9999

49 views

I'm planning on working on a puzzle game for awhile, because...well...I feel like working on a puzzle game. Plus I have a really cool idea for what it's going to play like.

But that isn't exactly why I'm posting this entry, well other than the fact that I like to make it a daily event. I wanted to share my new code for storing global variables. I'm pretty sure this is a common method. Also, I haven't actually compiled this, so it might have some errors. It is in this class:

template <class T>
class Globals
{
private:
std::map globals;
public:
void set(std::string key, T value) { globals[key] = value; }
T get(std::string key) { return globals[key]; }
std::map getList() { return globals; }
};



This class is so simplistic I'm quite sure it has to already be used.

So basically, you can do this:

Globals<int> numbers;
// Create some numbers
numbers.set("x", 5);
numbers.set("y", 7);
numbers.set("z", 10);
// Print the value of x
std::cout << "x is: " << numbers.get("x") << std::endl;
// Print all the numbers
for(std::mapint>::iterator i = numbers.getList().begin(); i != numbers.getList().end(); i++)
std::cout << i->first << " = " << i->second << endl;



This system is good because you can load the variables from a file.

I'm trying out STL maps for alot of different tasks and they seem to work well so far.
Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now