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. General Programming
  3. C / C++ / MFC
  4. fun(char*) and fun(char)

fun(char*) and fun(char)

Scheduled Pinned Locked Moved C / C++ / MFC
question
6 Posts 4 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.
  • B Offline
    B Offline
    bobi_zcl
    wrote on last edited by
    #1

    Hi: I overload funcion fun(char) and fun(char*),when I call fun(0),the compiler said:" ambiguous called ....",but when I call fun(1),the compiler will select fun(char).why? I also define fun(int) and fun(int*),to my surprise,I call fun(0) in the same way. The compiler compile successfully and select fun(int),Why? your friend:bobi

    A D 2 Replies Last reply
    0
    • B bobi_zcl

      Hi: I overload funcion fun(char) and fun(char*),when I call fun(0),the compiler said:" ambiguous called ....",but when I call fun(1),the compiler will select fun(char).why? I also define fun(int) and fun(int*),to my surprise,I call fun(0) in the same way. The compiler compile successfully and select fun(int),Why? your friend:bobi

      A Offline
      A Offline
      Abyss
      wrote on last edited by
      #2

      I'm not a compiler expert, but suppose that the compiler tries to convert (cast) the input argument to something. Since 0 is by default an integer it tries to cast to char but it also can be casted to NULL pointer. So the compiler doesn't know which is the best solution. In the second case the 0 is an integer and the compiler found the method, doesn't need conversion (casting). I suppose if you call the function as fun(0L) it will have the same result as in the previous case... To resolve the problem call the function as fun((char)0). Hope this help, Abyss

      T B 2 Replies Last reply
      0
      • A Abyss

        I'm not a compiler expert, but suppose that the compiler tries to convert (cast) the input argument to something. Since 0 is by default an integer it tries to cast to char but it also can be casted to NULL pointer. So the compiler doesn't know which is the best solution. In the second case the 0 is an integer and the compiler found the method, doesn't need conversion (casting). I suppose if you call the function as fun(0L) it will have the same result as in the previous case... To resolve the problem call the function as fun((char)0). Hope this help, Abyss

        T Offline
        T Offline
        toxcct
        wrote on last edited by
        #3

        i also though of an implicit cast (that is what the compiler does actually), but there should not be any difference between 0 and 1 in calling the function. if 0 is casted to char, so 1 should be too. if 0 is casted to char* (NULL pointer), then 1 should be casted to address 1...


        TOXCCT >>> GEII power
        [toxcct][VisualCalc]

        1 Reply Last reply
        0
        • A Abyss

          I'm not a compiler expert, but suppose that the compiler tries to convert (cast) the input argument to something. Since 0 is by default an integer it tries to cast to char but it also can be casted to NULL pointer. So the compiler doesn't know which is the best solution. In the second case the 0 is an integer and the compiler found the method, doesn't need conversion (casting). I suppose if you call the function as fun(0L) it will have the same result as in the previous case... To resolve the problem call the function as fun((char)0). Hope this help, Abyss

          B Offline
          B Offline
          bobi_zcl
          wrote on last edited by
          #4

          oH,dear: I try calling fun(0L),the compiler compile successfully and select fun(int) still. your friend:bobi

          1 Reply Last reply
          0
          • B bobi_zcl

            Hi: I overload funcion fun(char) and fun(char*),when I call fun(0),the compiler said:" ambiguous called ....",but when I call fun(1),the compiler will select fun(char).why? I also define fun(int) and fun(int*),to my surprise,I call fun(0) in the same way. The compiler compile successfully and select fun(int),Why? your friend:bobi

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            bobi_zcl wrote: I overload funcion fun(char) and fun(char*),when I call fun(0),the compiler said:" ambiguous called ....",but when I call fun(1),the compiler will select fun(char).why? Was this not answered sufficiently here and here? 0 is an integral constant expression. For the char/char* case, neither is a better match for the 0 than the other, hence the ambiguity error. For the int/int* case, fun(int) is an exact match for 0, thus no error.


            "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

            T 1 Reply Last reply
            0
            • D David Crow

              bobi_zcl wrote: I overload funcion fun(char) and fun(char*),when I call fun(0),the compiler said:" ambiguous called ....",but when I call fun(1),the compiler will select fun(char).why? Was this not answered sufficiently here and here? 0 is an integral constant expression. For the char/char* case, neither is a better match for the 0 than the other, hence the ambiguity error. For the int/int* case, fun(int) is an exact match for 0, thus no error.


              "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

              T Offline
              T Offline
              toxcct
              wrote on last edited by
              #6

              broken links... too bad X|


              TOXCCT >>> GEII power
              [toxcct][VisualCalc]

              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