| View previous topic :: View next topic |
| Author |
Message |
paupav Master Cheater
Reputation: 13
Joined: 15 Apr 2011 Posts: 314 Location: P. Sherman 42, Wallaby Way, Sydney
|
Posted: Mon Aug 25, 2014 9:54 am Post subject: accessing object names and class members in another .cppfile |
|
|
How can I do it? I need to order window to close in my sfml project.
RenderWindow is class that creates Window. Lets say that mywindow is object name of RenderWindow class. RenderWindow has function close() which obviously closes window. So how can I make that RenderWindow named "mywindow" close in some other .cpp file in which it wasn't declared? Is there any way?
I would close it by using commands window.close(); if I had to close it in same .cpp file in which it was declared.
P.S I'm talking about C++; |
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Mon Aug 25, 2014 1:07 pm Post subject: |
|
|
Define the object as a global and use extern to "import" it into another files scope.
Such as:
main.cpp:
| Code: |
#include <Windows.h>
#include "RenderWindow.h"
RenderWindow mywindow;
// ... other src here .. |
someothercpp.cpp:
| Code: |
#include "RenderWindow.h"
extern RenderWindow mywindow;
// .. other src here ..
|
_________________
- Retired. |
|
| Back to top |
|
 |
paupav Master Cheater
Reputation: 13
Joined: 15 Apr 2011 Posts: 314 Location: P. Sherman 42, Wallaby Way, Sydney
|
Posted: Mon Aug 25, 2014 2:37 pm Post subject: |
|
|
Ok, but that object holds stuff like window name and resolution.
this for some reason wont compile:
filethatactslikemain.hpp //but actually isn't main
| Code: | | sf::RenderWindow window; |
filethatactslikemain.cpp
| Code: | | window.create(sf::VideoMode(800, 600), "Test engine"); |
someotherfile.cpp
| Code: | | extern sf::RenderWindow window; |
all have others .hpp #included[/code] |
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
|
| Back to top |
|
 |
paupav Master Cheater
Reputation: 13
Joined: 15 Apr 2011 Posts: 314 Location: P. Sherman 42, Wallaby Way, Sydney
|
Posted: Mon Aug 25, 2014 3:35 pm Post subject: |
|
|
Thank you a lot!
I've noticed that It is possible to use mywindow.close()only in the original function where window was declared, and that it isn't possible to use it where mywindow was declared by "extern"
Is there any other way to do that so that I can use functions which you know? |
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Tue Aug 26, 2014 2:48 pm Post subject: |
|
|
| pauw wrote: | Thank you a lot!
I've noticed that It is possible to use mywindow.close()only in the original function where window was declared, and that it isn't possible to use it where mywindow was declared by "extern"
Is there any other way to do that so that I can use functions which you know? |
Store the object as as a pointer instead of just a static instance. You should be able to access the pointer from anywhere in the code and do whatever you need to it.
main.cpp:
| Code: |
sf::RenderWindow* window = NULL;
// somewhere in your code you would need:
window = new sf::RenderWindow();
// and be sure to cleanup!
delete window;
window = NULL; |
Then extern it like that as well:
someothercpp.cpp:
| Code: |
extern sf::RenderWindow* window;
// make use of it when needed..
if (window)
window->close();
|
_________________
- Retired. |
|
| Back to top |
|
 |
|