Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [ANSWERED] (I'm the the most stupid person on Earth)  (Read 2247 times)

0 Members and 1 Guest are viewing this topic.

Mark

  • Guest
[ANSWERED] (I'm the the most stupid person on Earth)
« 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?
« Last Edit: December 26, 2013, 08:31:06 pm by Mark »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Are you sure that you didn't write this instead?

sf::RenderWindow window();

... which is a function, not a variable.
Laurent Gomila - SFML developer

Mark

  • Guest
Wait a minute...
Edit: Thanks.
« Last Edit: December 26, 2013, 08:32:43 pm by Mark »

Raincode

  • Full Member
  • ***
  • Posts: 118
    • View Profile
Re: [ANSWERED] (I'm the the most stupid person on Earth)
« Reply #3 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()?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: [ANSWERED] (I'm the the most stupid person on Earth)
« Reply #4 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...
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Raincode

  • Full Member
  • ***
  • Posts: 118
    • View Profile
Re: [ANSWERED] (I'm the the most stupid person on Earth)
« Reply #5 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...
« Last Edit: December 27, 2013, 06:06:48 pm by Raincode »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: [ANSWERED] (I'm the the most stupid person on Earth)
« Reply #6 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;
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10846
    • View Profile
    • development blog
    • Email
AW: [ANSWERED] (I'm the the most stupid person on Earth)
« Reply #7 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Mark

  • Guest
Re: [ANSWERED] (I'm the the most stupid person on Earth)
« Reply #8 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...