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.
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.
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.
#include <iostream>
usingnamespace std;
class one {
int I;
public:
void setI() {
I = 1;
cout << I;
}
friendclass 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;
}
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...
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)
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.