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

Author Topic: Should you call events by reference?  (Read 2835 times)

0 Members and 1 Guest are viewing this topic.

Powereleven

  • Newbie
  • *
  • Posts: 36
    • View Profile
Should you call events by reference?
« on: January 28, 2018, 07:32:39 pm »
I was testing a button function that its implementations are not the subject of this post. It basically just calls the pollEvent and does something. the RenderWindow parameter must have the & to work, but the event parameter works with both (by reference and by value). Which one is preferable and why?

void button(sf::RenderWindow &app, sf::Event &e)
{
}

or

void button(sf::RenderWindow &app, sf::Event e)
{
}

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Should you call events by reference?
« Reply #1 on: January 28, 2018, 07:39:32 pm »
Do you understand what a reference is? Do you understand why the render window needs a reference?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Powereleven

  • Newbie
  • *
  • Posts: 36
    • View Profile
Re: Should you call events by reference?
« Reply #2 on: January 28, 2018, 07:51:58 pm »
I believe render window needs a reference so that when the function scope ends the alterations don't die there, because the & was used so the modifications were done to the object itself instead of a copy right?

Powereleven

  • Newbie
  • *
  • Posts: 36
    • View Profile
Re: Should you call events by reference?
« Reply #3 on: January 28, 2018, 07:55:18 pm »
So in the event object, in practice, it makes to no difference because the event is called inside the function and dies there (for example I tested with a mouse click triggering app.close). But which one should I use?
« Last Edit: January 28, 2018, 07:58:06 pm by Powereleven »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Should you call events by reference?
« Reply #4 on: January 28, 2018, 08:08:10 pm »
Maybe the better question would have been what happens when you don't use a reference. The answer is that you create a copy. Got the render window this is prohibited since it makes no sense to copy a window.
If it doesn't matter whether something gets copied or not then the question becomes more whether it's expensive to copy something.
In the case of sf::Event, it doesn't really matter as the to be copied size is relatively low and you will most certainly have enough available resources. If you however want to insist on no "wasted" resources, you may pass it as reference.

I know C++ is hard to grasp such concept in the beginning, if however you haven't read a good C++ book, I advise you to do at so. :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Powereleven

  • Newbie
  • *
  • Posts: 36
    • View Profile
Re: Should you call events by reference?
« Reply #5 on: January 28, 2018, 08:13:40 pm »
Is it common to have parameters sf::Event &e or people use a different technique?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Should you call events by reference?
« Reply #6 on: January 28, 2018, 08:48:48 pm »
It's quite common I'd say.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Should you call events by reference?
« Reply #7 on: January 28, 2018, 08:57:30 pm »
One important thing: if you want to optimize by avoiding a copy, you must use a const reference. A non-const reference must be used only when you really mean to modify the parameter inside the function.

And no, for sf::Event I don't think it makes sense to pass it by const reference, you're not going to optimize anything. People tend to abuse const references for anything that is more than a primitive type, I think it's wrong. A reference will involve an additional indirection everytime you access the variable, and also probably badly mess with cache, etc. So it's not just "copy or not copy".
Laurent Gomila - SFML developer