Help on understanding a piece of code

This is a discussion on Help on understanding a piece of code within the C++ Programming forums, part of the General Programming Boards category; I cant interpret this snippet of code. I suspect its if GetLastError() returns ERROR_SUCCESS then assign something to bRet But ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Location
    France
    Posts
    747

    Help on understanding a piece of code

    I cant interpret this snippet of code.
    I suspect its
    if GetLastError() returns ERROR_SUCCESS then assign something to bRet
    But where is the if() statement?
    Code:
    BOOL bRet=FALSE;
    bRet=(GetLastError() == ERROR_SUCCESS);
    Compiler MSVC++ 2010 with Code::Blocks.

  2. #2
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    452
    Can't tell without knowing how BOOL is defined. The return of "==" maybe automatically converted to BOOL. This can be risky. It's best to use C++ type "bool". Best guess is that BOOL is defined with int 0 and 1 or unsigned 0 and 1. Then C++'s bool true/false returned by "==" is converted to 0/1.
    Last edited by nimitzhunter; 05-11-2011 at 12:56 PM.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  3. #3
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    671
    BOOL is defined as an integral type and everybody who has ever touched WinAPI knows this. This isn't risky, it's good to stay with WinAPI types when writing Win code.
    bRet is assigned to the result of expression (GetLastError() == ERROR_SUCCESS). So, if the condition is true (read: error code equals 0) the return value is true (read: success), otherwise it is false (read: failure).
    Last edited by kmdv; 05-11-2011 at 01:30 PM.
    I never put signature, but I decided to make an exception.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Location
    France
    Posts
    747
    Thank you for the explanation!

    So it can be used instead of if() statement.
    Compiler MSVC++ 2010 with Code::Blocks.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Ducky View Post
    Thank you for the explanation!

    So it can be used instead of if() statement.
    Only in cases where you want a true/false value in a variable... it can't make the decision if() makes...

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    Think of it with a different kind of operation:
    Code:
    int a = 5 + 7;
    The "5 + 7" is evaluated and assigns the result (12) to "a".

    The == operator works the same way. It takes two operands, compares their equality, and evaluates to either 0 (false) or 1 (true):
    Code:
    int a = 5 == 7;
    5 does not equal 7, so "a" is assigned the value 0 (false).
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Location
    France
    Posts
    747
    Yes, thats right, i understand now, great, thank you!
    Compiler MSVC++ 2010 with Code::Blocks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. K&R2 - Piece of code not working.
    By reqonekt in forum C Programming
    Replies: 2
    Last Post: 11-13-2009, 10:07 AM
  2. what's wrong with this piece of code
    By studentc in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2006, 06:58 PM
  3. Help with a piece of code
    By Victor4015 in forum C++ Programming
    Replies: 1
    Last Post: 11-16-2005, 04:38 PM
  4. Help with a little piece of code
    By cdonlan in forum C Programming
    Replies: 5
    Last Post: 11-15-2004, 11:38 AM
  5. What is your favorite piece of code?
    By Yoshi in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 01-22-2002, 06:12 AM

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