Solved

i made an object and  i want to pass the opject to other class ? using c++

Posted on 2009-05-16
7
242 Views
Last Modified: 2012-05-07
i have 5 files
main.cpp
game.h    game .cpp
level.h   level.cpp
--------------------------------
so i made an object on the main.cpp for the level class  , and i want to pass the level object to the game.cpp so i can use the data that is stored in the level object

thanks

if you dont understand please say what you dont understand so i will try and explane more

thanks
0
Comment
Question by:mgtm3
  • 3
  • 3
7 Comments
 
LVL 12

Expert Comment

by:Let_Me_Be
ID: 24402133
Well, you can pass it same way you pass any other variable. Can you please describe what exactly are you having problem with?
0
 

Author Comment

by:mgtm3
ID: 24402144



lets say this

level.cpp

{

class level{

public:

int x;
 

};

}

////////////////////////////////////////

main.cpp{

level Level;

Level.x=5;

}

///////////////////////////////////////////////////////////

game.cpp{

// i want to pass the Level object from the main.cpp to here

how can i do this ?

}

Open in new window

0
 
LVL 12

Accepted Solution

by:
Let_Me_Be earned 250 total points
ID: 24402162
OK, the easiest way to access a variable from different file is to make it global and access it using the extern directive.
//main.cpp

#include "level.h"
 

Level level;
 

int main()

{

    do_something();

}
 

//other.cpp

#include "level.h"
 

extern Level level;
 

void do_something()

{

    level.x = 10;

}

Open in new window

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:mgtm3
ID: 24402230
its not working :(

i get a new object when i write
Level level;
in the new file
0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 250 total points
ID: 24402416
Pass level by reference into the constructor of Game and assign it to a member of game, which will be a reference to level. From this point on the game object will have a reference to level.

Note, there is no need (nor is it good practice) to have level as a file scoped object as shown above.
// Level class

class Level

{

};
 

// Game class

class Game

{

public:

   Game(Level & l) : l_(l) {} // Pass level into the constructor of game as a reference
 

private:

   Level & l_; // A reference to level as a member of game, initialised in the constructor

}
 

int main()

{

   Level l; // Create level

   Game g(l); // Create game and pass it level

}

Open in new window

0
 
LVL 12

Expert Comment

by:Let_Me_Be
ID: 24402459
Ok, i would again need a clarification what you are trying to achieve. You don't want to create a new object, but then I don't understand what object you want to access.

I general, if you want to share variable between several files, then you have several options.

First you can use a global variable which is then accessed by extern directive.

You can use a global static variable and an accessor function that returns reference to this variable and then use this function to access the variable.

And of course you can pass as a parameter of a call or constructor (but that doesn't seem to be what you are trying to achieve).

From what you have written, you may be interested in the Singleton pattern: http://en.wikipedia.org/wiki/Singleton_pattern
0
 

Author Comment

by:mgtm3
ID: 24402870
i made it work thanks :)
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ASP.NET Web API or ASP.NET Core MVC? 3 53
max float value 3 26
Advice on Xojo as a development tool over VB. 4 40
Capture logon name 13 48
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now