Thread: x++ and x++

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    16

    x++ and x++

    Why do we have x++ when in some cases it doesn't even work. Why can't we have just ++x. When do I use ++x and when do I use x++?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by etricity
    Why do we have x++ when in some cases it doesn't even work.
    What is the difference between x++ and ++x?

    Quote Originally Posted by etricity
    Why can't we have just ++x.
    We could, but we don't.

    Quote Originally Posted by etricity
    When do I use ++x and when do I use x++?
    If you are using them standalone in a statement, then you should always prefer ++x because if x is of a type for which both prefix and postfix forms of operator++ have been overloaded, then it is likely that the postfix version does more work than the prefix version, and may in fact call the prefix version.

    In other cases, refer to my previous question: what is the difference between x++ and ++x?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by etricity View Post
    Why do we have x++ when in some cases it doesn't even work.
    What do you mean?
    Of course it always works!
    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"

  4. #4
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    I think he meant, "doesn't work in the way that I intended it to."

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by MutantJohn View Post
    I think he meant, "doesn't work in the way that I intended it to."
    There is a saying about people who blame the tools they use when things go wrong.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed