?: operator

This is a discussion on ?: operator within the C++ Programming forums, part of the General Programming Boards category; How do you do use this? I sort of know what it does, but I don't understand how....

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    417

    ?: operator

    How do you do use this? I sort of know what it does, but I don't understand how.

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Condition ? TruePart : FalsePart

    It's mainly used for replacing if statements:
    Code:
    theNumber *= ( doDouble == true ? 2 : 1 );
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    99
    Code:
    int true=1;
    int test=0;
    
    test = (true==1) ? 1 : 0;
    cout << test << endl;
    The output would be 1. Read it like this, if test is equal to 1 then test equals 1, else test equals 0.

  4. #4
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Read it like this, if test is equal to 1 then test equals 1, else test equals 0.

    He meant "if true is equal to 1 then test equals 1, else test equals 0.
    Away.

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    99
    Oops...Thank you for the correction. Misstyped it and forgot to re-read my post.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 08:05 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 08:38 AM
  4. Destructor - Execution problem
    By triste in forum C++ Programming
    Replies: 16
    Last Post: 09-26-2004, 02:57 PM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-07-2003, 12:51 AM

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