Solved

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

Posted on 2009-05-16
7
243 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

822 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