can classes share variables

can classes share variables, for example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 class Example
{
void Reasons()
{
 int I = 1;
}
}
class OtherExample
{
void OtherReasons()
{
 I = I + 2;
}
}

obviously this doesn't work, but if it is possible to get them to share the variable I, how would I get that to happen.

1
2
3
4
5
6
7
8
9
10
void Example::Reasons()
{
 
}


void OtherExample::OtherReasons()
{

}

fallow up question, is there any reason to use classes like that.
I was trying to make a class that held global information, but I cant use that info I any other classes which kind of makes it useless.
Last edited on
yes, of course you can.
one way is various types of inheritance. the easy one is a has-a type inheritance:

class example
{
otherexample x;
int &I = x.I; //tie them together with a reference maybe? I must be public in otherexample though.
}

there are more complex ways to do it that cover more exotic needs... what *exactly* do you need to do?
I figured it would have something to do with inheritance. problem is I have not learned how to do that or what that means.

you know in the program I am working right now, I had this idea to use a pointer to move the value of a variable into different classes. is this inheritance or am I being stupid.
Last edited on
inheritance is where one class is 'inside' another. There are several forms of it, and its a lot to learn, so if you haven't seen it yet, it may not be a great place to start.

what you are doing is a simple form of shared memory. Its fine, if you manage it very carefully.

one way to manage it would be to have a master class:
class a
{
int I;
public:
int* geti() {return &I;}
}

and force construction of the other classes to work with a
class b
{
int *I;
b(int* x) {I = x;}
}

a avar;
b bvar(avar.geti());

avar and bvar share I now.

roughly. I may have borked something there doing it on the fly...
there are flaws to this kind of design -- be aware of this -- but with what you know to date, it may be your path forward for now. One of the themes of OOP is to NOT tightly couple classes like this... :P A quick example of how this could go horribly wrong would be if avar went out of scope and deconstructed, invalidating the pointers in the other classes, and you get a nasty crash or corruption.
Last edited on
What about this one:
1
2
3
4
5
6
7
8
9
class BaseClass {
public:
static int I;
};
int BaseClass::I;
class a : public BaseClass {
};
class b : public BaseClass {
};
Last edited on
that is what I ended up doing.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
using namespace std;

class one {
    int I;
    public:
    void setI() {
     I = 1;
     cout << I;
    }
    friend class two;
};

class two {
    public:
    void addToIt(one O) {
        O.I += 1;
        cout << O.I;
    }
};

int main()
{
 one oneClass; //declare a one
 oneClass.setI(); //function call sets value to variable I
 
 cout << endl;
 
 two twoClass; //declare a two
 twoClass.addToIt(oneClass); // can access the variable in one
 
 return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
int thisIsTheCommonVariable = 0;

class one {
public:
   int &i = thisIsTheCommonVariable;
};

class two {
public:
   int &i = thisIsTheCommonVariable;
};

int main()
{
   one a;
   two b;
   a.i = 10;
   std::cout << a.i << " " << b.i << '\n';
   b.i += 20;
   std::cout << a.i << " " << b.i << '\n';
}
Last edited on
the static and global solutions may get weird if you had multiple distinct one/two object pairs.
one a,b; //should a and b share I or each have a distinct I?
two x,y; //do all 4 objects share I or are there 2 of them?

you could make the static/global tied to a vector so each group tracked and held onto one location in the vector if you need to do that...
Last edited on
I was trying to make a class that held global information, but I cant use that info I any other classes

What do you mean by "can't" or "use in class"?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Player {
  std::string name;
  size_t age;
  // other code
};

class Game {
  Game( Player& );
  // other game;
};

int main() {
  Player john;
  Player bob;
  Player mary;
  // other code
  Game jackpot( bob );
}

The Player is a class that has some "global" information. We pass the player to the game. However, we have more than one player object. Each has different information. We could have given any of them to the game.

Have we failed, for our player objects do not obey the "There can be only one!" war-chant?

Why the Game could not "use" the Player?


The typical relationships are:
IS-A (inheritance)
HAS-A (composition)
Last edited on
Bro I already figured it out. What i meant by global was that I could have a class that held info that needed to be shared between multiple classes(which I later found out was called inheritance). This is only useful if you are going to have multiple classes, and in the end I found it easier to just shove everything into one class. But I have learned basic inheritance so this was still helpful anyway.
Topic archived. No new replies allowed.