0

Someone told me this is important to write:

if (10 == a.get())
{
}

instead of:

if (a.get() == 10)
{
}

Because C++ use pass-by-value for first parameter of == and use pass-by-reference for second parameter. Is this right ? and where is mentioned in c++ standard ?

Is order of objects important in conditional statements C++ ?

Anthony
  • 98
  • 7
  • Where did you get that idea about parameter-passing? (It's not right.) – molbdnilo Aug 11 '20 at 11:32
  • No no its not right. – Usama Tahir Aug 11 '20 at 11:33
  • 1
    No. The first version does just trigger a compile error when '=' is used instead of '=='. – knivil Aug 11 '20 at 11:33
  • 1
    It's an old thing, sometimes called Yoda convention. Back in the day, when compilers only compiled code and nothing else, this helped you prevent errors like `if (x=10)` (instead of `if(x==10)`). Nowadays any compiler will raise a warning when encountering assignment in if statement, and you in general want to treat warnings as errors, so it's not an issue anymore. And Yoda convention is less natural to read ("x equal to 10" vs. "10 equal to x"), so it's a rare sight in new code. – Yksisarvinen Aug 11 '20 at 12:00

0 Answers0