Thread: ++(variable) vs. (variable)++

  1. #1
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879

    ++(variable) vs. (variable)++

    Okay, I've been thinking about how (variable)++ works for a while. This is what I'm thinking:

    Code:
    (type) temp = (the_value);
    (the_value) = (the_value) + 1;
    return temp;
    And this is my idea of how ++(variable) works:

    Code:
    (the_value) = (the_value) + 1;
    return the_value;
    Am I right, or at least close to right? And if so, wouldn't (variable)++ be slower?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  2. #2
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455
    ++ is the same and += 1

  3. #3
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Yes, you're right. Infact, this is exactly how it works in operator overloading. Oh; and yes it is slower.. for obvious reasons.

    As an example:
    Code:
    // Postincrement eg. type++;
    const bit bit::operator++(int)
    {
    	bit returnBit = *this;
    	if (m_bit >= 32)
    		throw(std::out_of_range("bit::prev(), max already attained!"));	
    
    	++m_bit;
    	return returnBit;
    };
    
    // Preincrement eg. ++type;
    const bit bit::operator++()
    {
    	if (m_bit >= 32)
    		throw(std::out_of_range("bit::prev(), max already attained!"));	
    
    	++m_bit;
    	return *this;
    }
    Last edited by Eibro; 11-21-2002 at 08:47 PM.

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Ok, thanks My instructor guy told me that he always uses the pre-increment type in for loops since it's faster (marginally), but since most people around seem use the post-increment type, I sort of wondered.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Well in theory ++variable is faster, but keep in mind your compiler has the final word about this situation. I just played around with this (trying some code using both techniques) and the assembler that the compiler created for variable++ and ++variable was identical.
    Last edited by master5001; 11-22-2002 at 04:46 AM.

  6. #6
    Registered User nevermind's Avatar
    Join Date
    Oct 2002
    Posts
    23

    your right

    Yeah I dont think it matters that much in loops however makes a big difference in statements.

    Also thats where C++ comes from ... an increment to C

    Thought I'd share that
    "Cut the foreplay and just ask, man."

  7. #7
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    the assembler that the compiler created for variable++ and ++variable was identical.
    there goes my theory! lol oh well, I'll stick to ++variable anyways, just in case

    Also thats where C++ comes from ... an increment to C
    If it were up to me, I'd make it ++C
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Originally posted by Hunter2

    If it were up to me, I'd make it ++C
    lol

  9. #9
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    i perfer to use ++variable as it is slightly faster and matters in some applications i feel i might as well make it my habit.

  10. #10
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Originally posted by Hunter2
    there goes my theory! lol oh well, I'll stick to ++variable anyways, just in case
    Not really, it's up to the compiler to do these optimizations for you. Use preincrement when possible, don't leave it up to the compiler to fix your code for you.

  11. #11
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Ok

    But then, maybe the compiler screwed ++variable up instead improving variable++
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about printing a variable???
    By Hoser83 in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2006, 01:57 PM
  2. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 12:07 PM
  3. Replies: 2
    Last Post: 04-12-2004, 01:37 AM
  4. write Variable and open Variable and get Information
    By cyberbjorn in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 01:30 AM
  5. Variable question I can't find answer to
    By joelmon in forum C++ Programming
    Replies: 3
    Last Post: 02-12-2002, 04:11 AM