Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


accessing object names and class members in another .cppfile

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
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

PostPosted: Mon Aug 25, 2014 9:54 am    Post subject: accessing object names and class members in another .cppfile Reply with quote

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
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Mon Aug 25, 2014 1:07 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
paupav
Master Cheater
Reputation: 13

Joined: 15 Apr 2011
Posts: 314
Location: P. Sherman 42, Wallaby Way, Sydney

PostPosted: Mon Aug 25, 2014 2:37 pm    Post subject: Reply with quote

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
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Mon Aug 25, 2014 3:03 pm    Post subject: This post has 1 review(s) Reply with quote

You need to store the object in a .cpp not in a header.
_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
paupav
Master Cheater
Reputation: 13

Joined: 15 Apr 2011
Posts: 314
Location: P. Sherman 42, Wallaby Way, Sydney

PostPosted: Mon Aug 25, 2014 3:35 pm    Post subject: Reply with quote

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
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Tue Aug 26, 2014 2:48 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites