Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2009-05-16
7
Medium Priority
?
249 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 1000 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 1000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

971 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