-
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?
-
Are you sure that you didn't write this instead?
sf::RenderWindow window();
... which is a function, not a variable.
-
Wait a minute...
Edit: Thanks.
-
I'm just curious, what is the advantage of using
window->clear() etc.
compared to
window.clear()?
-
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...
-
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...
-
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;
-
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.
-
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...