John
John

Reputation: 53

Are my variables global?

Hello I've been working on a homework and due to homework rules im not allowed to use global variables. I've made a research about global variables but couldnt realy understand if my variables are global or local. The variables are defined in constructor inside my class. This is how my header looks like:

#include <string>
using namespace std;

class Team{
    public:
        string tColor;
        string tName;
};
class Player{
    public:
        string pPos;
        string pName;
};
class SocReg {
    private:
        Team *teams;// These are the variables Im not sure of
        Player *players;// These are the variables Im not sure of
        int playernum, teamnum; // These are the variables Im not sure of
    public:
        SocReg();
        ~SocReg();
        void addTeam( string teamName, string color );
        void removeTeam( string teamName );
        void addPlayer( string teamName, string playerName, string playerPosition );
        void removePlayer( string teamName, string playerName );
        void displayAllTeams();
        void displayPlayer( string playerName );
        void displayTeam( string teamName );
// ...
// you may define additional member functions and data members,
// if necessary.
};

This question might sound too noobish but im so confused thanks in advance

Upvotes: 1

Views: 104

Answers (3)

Tarnum
Tarnum

Reputation: 1

They are all class definitions, no variables declared.

So no global variables.

For example:

// example.cpp
int BAR;    
class foo {
    int bar;
};
foo FOO;

void f() {
    BAR = 0; // We can access global variable here
}

int main() {
    foo FOO2;
    BAR = -1; // We can access global variable here
    return 0;
}

BAR and FOO are global variables, they can be accessed globally.

class foo {
    int bar;
};

is merely definition of the class foo.

the FOO2 inside the main() function is also NOT a global variable, because it's inside the main function and could not be accessed by external functions.

Upvotes: 0

Christian Hackl
Christian Hackl

Reputation: 27518

    Team *teams;// These are the variables Im not sure of
    Player *players;// These are the variables Im not sure of
    int playernum, teamnum; // These are the variables Im not sure of

Pragmatic answer: These variables are neither global nor local. They are member variables. But whoever gave you this assignment surely just wanted to make sure that you don't use global variables, so you'll be fine. It would be completely meaningless to give students an assignment with a class and forbid member variables.

Language-lawyer answer: Neither "global variable" nor "member variable" are official terminology. Believe it or not, but the entire ~1300-1400 PDF for the ISO C++ standard contains only 3 instances of "member variable" and 1 instance of "global variable" (I have searched in a draft PDF, but that doesn't make much of a difference).

A local variable is described in the C++ standard at §3.3.3/1 as follows:

A variable declared at block scope is a local variable.

A global variable is officially called "non-local variable" (§3.6.2). It's by definition the opposite of a local variable.

A member variable is officially called a "data member", as evidenced in §9.2/1:

Members of a class are data members, member functions (...), nested types, and enumerators.

Upvotes: 4

Matt Timmermans
Matt Timmermans

Reputation: 59144

Global variables are things you can see and change from "anywhere" in the code, i.e, outside of any particular class.

They are considered harmful, because when you change a global variable, you have to somehow know all the code that might look at it, in order to know what effects that change might have. That makes it very difficult to reason about your code.

All of your variables are encapsulated within objects and are NOT global. In C++, global variables are declared outside of classes, or with "static".

Upvotes: 2

Related Questions