Sign in to follow this  
redhaze

a little help with c++ classes

Recommended Posts

ok im working on game and everything seem to be going well till i decided to make my program more moduler. now the problem i have is that i create a class in a header file to be use by other programs or cpp that create. then in one of the cpp i created an instrance of the class and gave the objects some values. now these value i want them to be used in another cpp i create for the same program but everything i tried doesnt seem to work. here an example of what im trying to do: ////////////////////////////////////////////////// //**program1.h #ifndef program1_h #define program1_h class player { public: player(); //constructor ~player(); //destructor int x; }; #endif ////////////////////////////////////////////////////// //**program1.cpp #include<iostream> #include"program1.h" using namespace std; player::player() { } player::~Player() { } player game // instance of the class player //////////////////////////////////////////////// void gamefunc(void) { game.x=10; //give game.x a value } /////////////////////////////////////////////////// //**program2.cpp #include<iostream> #include"program1.h" using namespace std; //////////////////////////////////////////////// void miscfunc(void) { cout<<game.x<<endl; //print out game.x } any ideas on a solution? this is my first atemped at classes.

Share this post


Link to post
Share on other sites
What you describe is called a global variable, and the usage of extern. Look these up in your favourite C++ reference, or here. These can make debugging or writing new code using them harder, because they can be modified by almost everything in your program. See also the discussion here.

Share this post


Link to post
Share on other sites
ok i took a look at the links you have posted but i still have found the answer to my problem. let put it like this i create a class in a header file to be use by multiply projects, then in one of the projects i create an instance of the class.
in that instance i gave the objects of the class values. now what i want to do is use the instance i created with the values i gave them in another project but here is where i run into problems. you see the only i can use the instance with the same name is if i static the instance in the second project but i wont have access to the values i gave the first instance i created.

do get what im trying to do now?

Share this post


Link to post
Share on other sites
You may not share instances of a class across different projects, although you may save the state of an instance in one project and load it in the other. How you do it depends on where and when the projects run: using a file, a shared memory buffer or a network interface as temporary storage are all possible.

This is because by default, every project creates its own memory area with its own class instances inside whenever it is run.

Share this post


Link to post
Share on other sites
i understand now and figure out a solution. i didnt know that instances cant be share but now i do. so what im planning on doing is just creating a global variable and swap the values of the instances with the global variable then swap the second instrance from another project with the global variable. does that sound ok or is there a more effiencient way?

either way thanks because now i at least have a better understanding of what i was doing wrong.

Share this post


Link to post
Share on other sites
If the two programs are running simultaneously, you could simply have them create some shared memory buffer using your operating system API, and store the variable in this memory buffer, with the required mutexes to handle concurrence.

By sheer curiosity, why do you need to do this? What will be the two programs, and what data will be shared?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this