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

Author Topic: why not use const and send by reference whenever possible  (Read 3253 times)

0 Members and 1 Guest are viewing this topic.

hajacken

  • Newbie
  • *
  • Posts: 11
    • View Profile
why not use const and send by reference whenever possible
« on: February 23, 2009, 06:39:27 pm »
Hi everyone, sometimes when I look at others people source code do I see that they only send by reference when it is only a large datatype as an image/string/array, yeah you know. The question I wonder is that why do people not send by reference whenever possible, even if it is a small datatype as an int? It will not be a huge performance gain but it will be faster right? So why do people not do it? In my opinion do the code looks much better too when a function send by reference and uses const whenever possible. It requires so little by the programmer. Do the compiler fix it or what have I forgot? Why do not SFML send by reference whenever possible?

Ceylo

  • Hero Member
  • *****
  • Posts: 2325
    • View Profile
    • http://sfemovie.yalir.org/
    • Email
Re: why not use const and send by reference whenever possibl
« Reply #1 on: February 23, 2009, 06:43:49 pm »
Quote from: "hajacken"
It will not be a huge performance gain but it will be faster right?

Waiting for you to check this :P .
Want to play movies in your SFML application? Check out sfeMovie!

hajacken

  • Newbie
  • *
  • Posts: 11
    • View Profile
why not use const and send by reference whenever possible
« Reply #2 on: February 23, 2009, 06:46:57 pm »
and with that do you mean?

Ceylo

  • Hero Member
  • *****
  • Posts: 2325
    • View Profile
    • http://sfemovie.yalir.org/
    • Email
why not use const and send by reference whenever possible
« Reply #3 on: February 23, 2009, 06:49:34 pm »
Quote from: "hajacken"
and with that do you mean?

Uh ? I don't understand, sorry :/
Want to play movies in your SFML application? Check out sfeMovie!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
why not use const and send by reference whenever possible
« Reply #4 on: February 23, 2009, 06:50:07 pm »
Receiving (and not sending ;)) by const reference is purely an optimization, in order to avoid unnecessary copies. The "clean" solution is to pass by value when you don't really need the original object.

Regarding performances, references are pointers so:
- It "wastes" memory when the data size is less than pointer size
- It "wastes" CPU because you get an extra indirection

That's why it's useless for small types.
Laurent Gomila - SFML developer

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
why not use const and send by reference whenever possible
« Reply #5 on: February 23, 2009, 06:51:14 pm »
Quote
and with that do you mean?

He means "prove it".

Quote
Uh ? I don't understand, sorry :/

He means he didn't understand what you said.

Sorry if there's a mistake in my translation :lol:
Laurent Gomila - SFML developer

hajacken

  • Newbie
  • *
  • Posts: 11
    • View Profile
why not use const and send by reference whenever possible
« Reply #6 on: February 23, 2009, 07:21:16 pm »
Thanks Laurent for the explanation. And the language stuff, I do my best but English if not my strongest side as you have already noticed.