SFML community forums

Help => General => Topic started by: Mark on December 26, 2013, 07:40:45 pm

Title: [ANSWERED] (I'm the the most stupid person on Earth)
Post by: Mark on December 26, 2013, 07:40:45 pm
I want this:
sf::RenderWindow window;
sf::RenderWindow *ptr_window;
ptr_window = &window;
ptr_window->clear();
But I get the following:
cannot convert 'sf::RenderWindow (*)()' to 'sf::RenderWindow*' in assignment.
Anyone?
Title: Re: How to pass an adress of sf::RenderWindow to a pointer? (really need answer!)
Post by: Laurent on December 26, 2013, 08:25:08 pm
Are you sure that you didn't write this instead?

sf::RenderWindow window();

... which is a function, not a variable.
Title: Re: How to pass an adress of sf::RenderWindow to a pointer? (really need answer!)
Post by: Mark on December 26, 2013, 08:27:53 pm
Wait a minute...
Edit: Thanks.
Title: Re: [ANSWERED] (I'm the the most stupid person on Earth)
Post by: Raincode on December 27, 2013, 12:11:00 pm
I'm just curious, what is the advantage of using
window->clear() etc.
compared to
window.clear()?
Title: Re: [ANSWERED] (I'm the the most stupid person on Earth)
Post by: Nexus on December 27, 2013, 01:36:20 pm
You don't have a choice. Only one expression is valid, depending on whether you have a pointer or not. You should probably learn C++ with a good book, these are really basics you have to understand before using SFML...
Title: Re: [ANSWERED] (I'm the the most stupid person on Earth)
Post by: Raincode on December 27, 2013, 04:42:33 pm
Ok, maybe my question wasn't very precise... I am aware that one is with pointers and one with the object itself.
What I really meant is, why would you do

sf::RenderWindow window;
sf::RenderWindow *ptr_window;
ptr_window = &window;
ptr_window->clear();

insteand of
sf::RenderWindow window;
window.clear();
 

Edit: Well ehm, I think I just found the answer to my question while designing a game, so never mind...
Title: Re: [ANSWERED] (I'm the the most stupid person on Earth)
Post by: Nexus on December 27, 2013, 08:26:18 pm
What I really meant is, why would you do
You wouldn't. If you need pointers, you mostly don't keep them in the same scope as the original object. You usually pass them to functions, store them as member variables or inside a container.

By the way, I wouldn't write this code
sf::RenderWindow *ptr_window;
ptr_window = &window;
anyway. You can initialize variables directly:
sf::RenderWindow* ptr_window = &window;
Title: AW: [ANSWERED] (I'm the the most stupid person on Earth)
Post by: eXpl0it3r on December 27, 2013, 08:35:48 pm
The use of raw pointers is limited to situations where the pointer doesn't own the object itself (for that we have smart pointers) and reference can't be used. The reasons are, that raw pointers own objects have a very high chance of leaking memory, where as RAII constructs such as smart pointers automatically release memory after destruction. References can't be a nullptr, thus the chance of accessing a non existing object gets reduced.

With that one should see raw ppinters only very rarely, especially in simple games.
Title: Re: [ANSWERED] (I'm the the most stupid person on Earth)
Post by: Mark on December 27, 2013, 08:43:27 pm
Ok, maybe my question wasn't very precise... I am aware that one is with pointers and one with the object itself.
What I really meant is, why would you do

sf::RenderWindow window;
sf::RenderWindow *ptr_window;
ptr_window = &window;
ptr_window->clear();

insteand of
sf::RenderWindow window;
window.clear();
 

Edit: Well ehm, I think I just found the answer to my question while designing a game, so never mind...

This was an example that was designed to fit in as little code as possible. The true code uses the pointer as a protected variable of a menu class, so that the subclasses of menu (namely button class) would know where to draw their sprites...