Solved

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

Posted on 2009-05-16
7
245 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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 viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

739 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