Global variabes and class variables

So everyone knows that global variables are bad (most of the time), but is it ok to put a variable in a class to acces to it indirectly from any part of the program?
Last edited on
why wouldnt it be?
So i can do that and the program won't go function by function changing the value, right?
(Pretty obvious, but i still ask)
The program does what you tell it to do.
> So everyone 'knows' that global variables are VERY bad ..

Do people who say that really 'know'?

It is not global variables per se, but variables with global visibility and access that results in tight coupling.
Global variables with visibility restricted to a single translation unit is a staple of well designed C/C++ software.

1
2
3
4
5
6
7
// a.h
class A
{
    // ...
    private: static int v ; // progrmmatically visible to the entire program 
    // ... 
};


1
2
3
4
5
//a.cc

int A::v = 45 ;

// A implementation 



1
2
3
4
5
6
7
// better_a.h
class better_A 
{
    // ...
    // move 'v' to the implementation file with internal linkage 
    // ... 
};


1
2
3
4
5
6
7
8
9
10
// better_a.cc
namespace 
{
    int v = 45 ; // visible and accessible to the implementation of better_a
    // progrmmatically invisible to the rest of the program 
    // in C, this would be: static int v = 45 ; at global scope 
}

// better_a implementation
// the implementation can access 'v' 

using getters and setters helps to keep a lot of variables out of the way (in memory) until you need them.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Entity{
private:
    //member variables
    int m_ID;
public:

    //constructors

    int getID()
    {
        return m_ID;
    }

    void setID(int id)
    {
        m_ID = id;
    }

    //other functions

};


Regards,
Anthony
Last edited on
I know that in real life people use functions to set and use variables inside classes instead of making them public, is it ok to put the score of my game in a class at the starting of the program to use it on all the functions I want? That's why I ask this.
Last edited on
> is it ok to put the score of my game in a class to use it on all the functions I want?

Yes.

Provide a narrow and a (restricted) wide interface, perhaps. Something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// score_keeper.h

class score_keeper
{
    // everyone can read the score
    public: static int score() ;

    // only some can modify the score
    private: static void score( int new_value ) ;

    // these classes are allowed to modify the score
    friend class game ;
    friend class another_class_allowed_to_modify_the_score ;
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// score_keeper.cc

#include "score_keeper.h"

namespace
{
    int current_score = 0 ;
}

int score_keeper::score() { return current_score ; }

void score_keeper::score( int new_value )
{
    // validate new_value
    current_score = new_value ;
}

Edit: Oh, ok, now I understand it, but i was thinking in something like:

1
2
3
4
5
6
7
8
9
class Score {
  private:
    int score;

  public:
    void Increase(int Value);
    int Get();
    void Reset();
}


//Blah, blah, blah

1
2
3
4
5
6
7
8
9
10
11
void Score::Increase(int Value){
    score += Value;
}

int Score::Get(){
    return score;
}

void Reset(){
    score=0;
}
Last edited on
closed account (o3hC5Di1)
Hi there,

The advantage of JLBorges approach is first and foremost that you don't even have to create a Score object, because it uses static members.
This means that you won't have to pass the Score object around all the time, if you're in a befriended class, you can simply get and set the score through score_keeper::score().
Please do correct me if I'm wrong here anyone- no expert myself.

Static member data and friend classes are slightly more advanced OOP though, so don't worry that you didn't think of it.

Hope that helps, let us know if you have any further questions.

All the best,
NwN
Topic archived. No new replies allowed.