2

I'm new to C++ and am making a game to teach myself more.

I would like to hear some of your thoughts in regard to storing/accessing large amounts of variables.

What i've done so far is tried to stick my 'critical' variables into separate namespaces so that i can access them throughout my program.

Is this a good idea? I have a strong feeling that it may come back to bite me.

Thanks in advance =]

EDIT: I'm a computing student in first year but i have a few years experience with Java.. C++ is so different =/

  • 7
    Large number of variables in itself is a bad idea. You should group your related variables into classes. – john Nov 02 '12 at 13:37
  • 2
    We all jumped in when we began programming and tried to create things like games - but it really is a good idea to start with basic tutorials about how things are done. It saves headache and heartache later on. Especially if you want to use C++... – Caribou Nov 02 '12 at 13:53
  • you can make small container or class to store stats, speed, radius of vision and etc. – Denis Ermolin Nov 02 '12 at 14:03
  • possible duplicate of [C++ Design Pattern for Passing a Large Number of Parameters](http://stackoverflow.com/q/5551640/1025391) – moooeeeep Nov 02 '12 at 14:08

2 Answers2

1

group your variables into structs or classes. If you need to expose large amount of variables outside your class then you have to reconsider your design

Gianluca Ghettini
  • 11,129
  • 19
  • 93
  • 159
1

Your main weapon in organising your data is classes: a class is an object that represents an element of your program. You name it, give it variables and functions, and then create instances of it in your program. The class is responsible for all of its data and can prevent other classes from accessing it.

class foo
{
    public:
    int iAmAPublicVariable; // <---This variable can be accessed by anyone who has access to an instance of the class

    private:
    int iAmAPrivateVariable; // <---- This variable can be accessed only from within the class itself.
};

One good technique to control access to your class data is through the use of Getters and Setters. So...

class foo
{
    public:
    int getImportantData();
    void setImportantData(int );

    private:
    int importantData;
};

int foo::getImportantData()
{
    //here I could put some validation to make sure that it's ok to give out importantData;
    return importantData; //this will return a copy of importantData, so the original is still safe within the class
}

void foo::setImportantData(int newData)
{
    //here I could put some validation to make sure that it's ok to overwrite importantData;
    importantData = newData;
}

With this setup the only way that importantData can be accessed is through the get and set methods, and so the class has ultimate control over what happens to it.

Classes should be the foundation of your program; if you've got a huge amount of variables then look at what they're for and what functions use them, and try to break the process down into discrete areas of functionality. Then create classes that represent these areas and give them the variables and methods they need. Your code should end up being smaller, easier to understand, more stable, easier to debug, more reusable, and a better representation of whatever it is you're trying to model.

Ian
  • 450
  • 4
  • 18