Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. Clever Code
  4. Comparing apples to oranges, or strings to bools

Comparing apples to oranges, or strings to bools

Scheduled Pinned Locked Moved Clever Code
question
4 Posts 4 Posters 13 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    ClementsDan
    wrote on last edited by
    #1

    I spent this afternoon trying to figure out why my code was throwing an exception. I tracked it down to the line

    if (strProtocol == FALSE)

    which should have been

    if (strProtocol.IsEmpty() == FALSE)

    So why did the original compile in the first place? Because FALSE is #defined as 0, and coincidentally so is NULL, and our string class has an operator==(const char*) — which does not properly check for NULL.

    D P 2 Replies Last reply
    0
    • C ClementsDan

      I spent this afternoon trying to figure out why my code was throwing an exception. I tracked it down to the line

      if (strProtocol == FALSE)

      which should have been

      if (strProtocol.IsEmpty() == FALSE)

      So why did the original compile in the first place? Because FALSE is #defined as 0, and coincidentally so is NULL, and our string class has an operator==(const char*) — which does not properly check for NULL.

      D Offline
      D Offline
      DaveAuld
      wrote on last edited by
      #2

      I was caught out with a very similiar thing. The feedback i got was that what you are doing is comparing the objects are the same object (i.e. they reference the same thing) as oppossed to comparing the objects state. So the compiler is happy. Now whether that is right or not is another question for someone smarter than me!

      Dave Who am I?: http://www.bebo.com/daveauld/ or http://www.dave-auld.net/

      L 1 Reply Last reply
      0
      • D DaveAuld

        I was caught out with a very similiar thing. The feedback i got was that what you are doing is comparing the objects are the same object (i.e. they reference the same thing) as oppossed to comparing the objects state. So the compiler is happy. Now whether that is right or not is another question for someone smarter than me!

        Dave Who am I?: http://www.bebo.com/daveauld/ or http://www.dave-auld.net/

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        I think the problem comes down to the fact that the value of FALSE is evaluated by the preprocessor (I'm assuming this is C++) so the actual code compiler sees if (strProtocol == 0) which is a valid statement. The programmer distinguishes between NULL and FALSE, the compiler doesn't.

        1 Reply Last reply
        0
        • C ClementsDan

          I spent this afternoon trying to figure out why my code was throwing an exception. I tracked it down to the line

          if (strProtocol == FALSE)

          which should have been

          if (strProtocol.IsEmpty() == FALSE)

          So why did the original compile in the first place? Because FALSE is #defined as 0, and coincidentally so is NULL, and our string class has an operator==(const char*) — which does not properly check for NULL.

          P Offline
          P Offline
          peterchen
          wrote on last edited by
          #4

          It sucks that the correct solutions are always rather complex[^]

          Personally, I love the idea that Raymond spends his nights posting bad regexs to mailing lists under the pseudonym of Jane Smith. He'd be like a super hero, only more nerdy and less useful. [Trevel]
          | FoldWithUs! | sighist

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups