Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Compilator missing errors...

Compilator missing errors...

Scheduled Pinned Locked Moved Unsolved C++ Gurus
6 Posts 3 Posters 1.5k Views
  • 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.
  • M Offline
    M Offline
    michelson
    wrote on last edited by
    #1

    ...or am I wrong here? Having code:

    int method(char c)
    {
      int i = -1;
      if(c == char(0xa7))
      {
         int i = static_cast<int>(c) + 1;
      }
      else
      {
         i = -2;
      }
      return i;
    }
    

    notice redeclaration of the i var.
    Is this legit? Cause it goes through compilation w/o errors but because of this code gives wrong results.

    1 Reply Last reply
    0
    • W Offline
      W Offline
      Wurgl
      wrote on last edited by
      #2

      Which "i" var do you mean? The one at the beginning of the method or the other one which exists only inside the if-block and gets lost at the end of the "if".

      1 Reply Last reply
      0
      • M Offline
        M Offline
        michelson
        wrote on last edited by michelson
        #3

        So it is valid code?
        There is no redeclaring here?
        I thought that i exists in method "scope" so since if is inside method the i already exist in this "scope" so there should be notification about redeclaration of variable.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          Your if body is a new scope that will end at the closing curly brace, so technically nothing wrong with re-declaring a variable with the same name except it makes your code confusing and won't give you the result you expect.

          You might get a shadowing warning though.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          W 1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

            Your if body is a new scope that will end at the closing curly brace, so technically nothing wrong with re-declaring a variable with the same name except it makes your code confusing and won't give you the result you expect.

            You might get a shadowing warning though.

            W Offline
            W Offline
            Wurgl
            wrote on last edited by
            #5

            @SGaist Shadowing warning? I know this warning (with gcc) only, when a parameter of a method gets hidden by some local variable in the code. Well, other compiler may print out different warnings.

            With this example the compiler finds something, that might not be what you (the thread starter) really want …

            g++ -W -Wall -Wextra -c example.cpp
            example.cpp: In function ‘int method(char)’:
            example.cpp:6:10: warning: unused variable ‘i’ [-Wunused-variable]
                  int i = static_cast<int>(c) + 1;
                      ^
            

            In general it is a good idea to turn on all warnings, read those warnings and try to understand them.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Wurgl, that's why I used "might" ;)

              In any case, I agree with you, all warnings on is a good habit.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              2

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved