Solved

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

Posted on 2009-05-16
7
241 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

762 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

22 Experts available now in Live!

Get 1:1 Help Now