Quick Question +=

This is a discussion on Quick Question += within the C++ Programming forums, part of the General Programming Boards category; Hey, I came across this += Tried searching the i-net but nothing came up? So what does this do? From ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    223

    Cool Quick Question +=

    Hey,

    I came across this +=

    Tried searching the i-net but nothing came up? So what does this do?

    From where I saw it my guess is:

    instead of x = x + y;
    you have x += y;

    Is this right?

    Are there any drawbacks etc. to using this? What are the advantages?

    Cheers

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,490
    AFAIK ..no drawbacks..
    As for advantages...IS there anything other than "Cleaner code" ?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    611
    From where I saw it my guess is:

    instead of x = x + y;
    you have x += y;

    Is this right?
    Why on Earth would you:
    Open browser
    Type/click to visit here
    Login
    Click new post
    Type your post
    Click post
    Logout

    and not
    Create file/Open IDE
    Type a 6 line test program
    Compile
    Observe output

    You searched, which is laudable, but then you gave up! Wikipedia may frown on original research, but software develoment isn't wikipedia. Paraphrasing somebody in a different thread
    Just try things out. The world wont end if you try something and it doesn't work.
    Are there any drawbacks etc. to using this? What are the advantages?
    It's exactly the same as writing 5+5+5 as 5*3 except for objects where += can be overloaded seperately from +
    Last edited by adeyblue; 05-11-2011 at 01:04 PM.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,264
    When the types of x and y are a class or some kind, then the operator += is typically more efficient than using operator + because no temporary objects will be created.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    223
    Thanks for the productive posts!!

    As for adeyblue... why even bother replying (SMH)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. quick question
    By mkeisu in forum C++ Programming
    Replies: 20
    Last Post: 05-14-2008, 09:21 AM
  2. A quick question
    By abh!shek in forum C++ Programming
    Replies: 22
    Last Post: 04-20-2008, 11:14 AM
  3. Quick question
    By mattio87 in forum C Programming
    Replies: 9
    Last Post: 05-04-2007, 05:19 AM
  4. Quick Question!
    By dizz in forum C++ Programming
    Replies: 14
    Last Post: 11-21-2002, 06:02 AM
  5. Quick question
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-05-2002, 11:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21