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. Bugs in translating code

Bugs in translating code

Scheduled Pinned Locked Moved Clever Code
helpc++delphi
10 Posts 7 Posters 0 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.
  • A Offline
    A Offline
    alexey N
    wrote on last edited by
    #1

    The last my task was to translate code from Delphi to c++. Here is the worst bug: if you have Delphi code if (count = 1) { ... } In c++ you must replace it with if (count == 1) { ... } else you'll got assignment in condition. But if you write constant first the compiler alert you if you leave "=" uncanged : if (1 = count)// Compiler error { ... } This trick helps me a lot.

    My english is not so good. Please, correct my errors. Best regards, Alexey.

    S P L A 4 Replies Last reply
    0
    • A alexey N

      The last my task was to translate code from Delphi to c++. Here is the worst bug: if you have Delphi code if (count = 1) { ... } In c++ you must replace it with if (count == 1) { ... } else you'll got assignment in condition. But if you write constant first the compiler alert you if you leave "=" uncanged : if (1 = count)// Compiler error { ... } This trick helps me a lot.

      My english is not so good. Please, correct my errors. Best regards, Alexey.

      S Offline
      S Offline
      Sarath C
      wrote on last edited by
      #2

      This is also applicable for "!="

      -Sarath_._ "Great hopes make everything great possible" - Benjamin Franklin

      My blog - Sharing My Thoughts, An Article - Understanding Statepattern

      B 1 Reply Last reply
      0
      • S Sarath C

        This is also applicable for "!="

        -Sarath_._ "Great hopes make everything great possible" - Benjamin Franklin

        My blog - Sharing My Thoughts, An Article - Understanding Statepattern

        B Offline
        B Offline
        Brady Kelly
        wrote on last edited by
        #3

        Not really because != can't result in an assignment, although for consistency one should apply the tip to != as well.

        S 1 Reply Last reply
        0
        • B Brady Kelly

          Not really because != can't result in an assignment, although for consistency one should apply the tip to != as well.

          S Offline
          S Offline
          Sarath C
          wrote on last edited by
          #4

          What if forget to add "!" before "="?

          -Sarath_._ "Great hopes make everything great possible" - Benjamin Franklin

          My blog - Sharing My Thoughts, An Article - Understanding Statepattern

          B 1 Reply Last reply
          0
          • S Sarath C

            What if forget to add "!" before "="?

            -Sarath_._ "Great hopes make everything great possible" - Benjamin Franklin

            My blog - Sharing My Thoughts, An Article - Understanding Statepattern

            B Offline
            B Offline
            Brady Kelly
            wrote on last edited by
            #5

            Very good point. Thanks for bringing it up.

            S 1 Reply Last reply
            0
            • B Brady Kelly

              Very good point. Thanks for bringing it up.

              S Offline
              S Offline
              Sarath C
              wrote on last edited by
              #6

              Another one is strcmp or CString's Compare function. People often used to forget equate to 0 for successful case. last week I have reviewed some source code of my colleagues. some of them haven't noticed this :(

              -Sarath_._ "Great hopes make everything great possible" - Benjamin Franklin

              My blog - Sharing My Thoughts, An Article - Understanding Statepattern

              1 Reply Last reply
              0
              • A alexey N

                The last my task was to translate code from Delphi to c++. Here is the worst bug: if you have Delphi code if (count = 1) { ... } In c++ you must replace it with if (count == 1) { ... } else you'll got assignment in condition. But if you write constant first the compiler alert you if you leave "=" uncanged : if (1 = count)// Compiler error { ... } This trick helps me a lot.

                My english is not so good. Please, correct my errors. Best regards, Alexey.

                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #7

                That was one of the rules in the coding standards at a place used to work. I went along with it, but my counter arguments are: 1) That only works when intending to compare with a non-L-value. 2) If you have the presence of mind to write it that way, you probably aren't going to mess it up anyway. Eventually they relaxed the rule. This continues to be why I would prefer the assignment operator to be :=, and a bare = be invalid.

                M 1 Reply Last reply
                0
                • A alexey N

                  The last my task was to translate code from Delphi to c++. Here is the worst bug: if you have Delphi code if (count = 1) { ... } In c++ you must replace it with if (count == 1) { ... } else you'll got assignment in condition. But if you write constant first the compiler alert you if you leave "=" uncanged : if (1 = count)// Compiler error { ... } This trick helps me a lot.

                  My english is not so good. Please, correct my errors. Best regards, Alexey.

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

                  alexey N wrote:

                  In c++ you must replace it with

                  alexey N wrote:

                  else you'll got assignment in condition.

                  Or, depending on the compiler and warning level, a warning. regards

                  1 Reply Last reply
                  0
                  • P PIEBALDconsult

                    That was one of the rules in the coding standards at a place used to work. I went along with it, but my counter arguments are: 1) That only works when intending to compare with a non-L-value. 2) If you have the presence of mind to write it that way, you probably aren't going to mess it up anyway. Eventually they relaxed the rule. This continues to be why I would prefer the assignment operator to be :=, and a bare = be invalid.

                    M Offline
                    M Offline
                    Michael Dunn
                    wrote on last edited by
                    #9

                    PIEBALDconsult wrote:

                    1. If you have the presence of mind to write it that way, you probably aren't going to mess it up anyway.

                    But when you do mess up and write = instead of ==, the compiler catches the mistake right away. That's the whole point - the bug never makes it into the compiled code, not even once.

                    --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ

                    1 Reply Last reply
                    0
                    • A alexey N

                      The last my task was to translate code from Delphi to c++. Here is the worst bug: if you have Delphi code if (count = 1) { ... } In c++ you must replace it with if (count == 1) { ... } else you'll got assignment in condition. But if you write constant first the compiler alert you if you leave "=" uncanged : if (1 = count)// Compiler error { ... } This trick helps me a lot.

                      My english is not so good. Please, correct my errors. Best regards, Alexey.

                      A Offline
                      A Offline
                      Ami Bar
                      wrote on last edited by
                      #10

                      In C# you cannot do this mistake, since any expression evaluted in an if statement must be boolean. So if you write: if (count = 1) {...} The compiler will error. The only exception is when you write: if (flag = false) {...} or if (flag = true) {...} The compiler won't error for this, but from my point of view there is no reason to compare booleans with an equal sign. Just use if(!flag) or if(flag). Ami

                      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