overloaded function question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • manju23reddy
    New Member
    • Aug 2008
    • 7

    #1

    overloaded function question

    consider overloaded functions
    //function 1
    void hi(int a)
    {
    }

    //function 2
    void hi(int a, int b = 20)
    {
    }

    int main()
    {
    hi(10);
    }

    which function will it be invoked and y.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Please give your topics proper titles; 'hi' is not a proper subject title.
    I have changed your other topic titles (all were named 'hi'). Don't do that anymore.

    kind regards,

    Jos (mod)

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Well I would suggest you try compiling your example and see what it does. Just add some cout statements to you functions so you can see which one is called.

      If you manage to get you code to compile without error then you can run it and find out which one is called then you can begin to attempt an explanation of why.

      Comment

      • manju23reddy
        New Member
        • Aug 2008
        • 7

        #4
        Originally posted by Banfa
        Well I would suggest you try compiling your example and see what it does. Just add some cout statements to you functions so you can see which one is called.

        If you manage to get you code to compile without error then you can run it and find out which one is called then you can begin to attempt an explanation of why.
        thanks for ur reply i tried this will raise to compiler error informing ambiguity.

        Comment

        • manju23reddy
          New Member
          • Aug 2008
          • 7

          #5
          Originally posted by JosAH
          Please give your topics proper titles; 'hi' is not a proper subject title.
          I have changed your other topic titles (all were named 'hi'). Don't do that anymore.

          kind regards,

          Jos (mod)
          hi iam new to this forum so i will post a proper one ...

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Originally posted by manju23reddy
            thanks for ur reply i tried this will raise to compiler error informing ambiguity.
            Correct :-) and that is your answer, the question which one will be called is pointless because the compiler will not compile it so neither will be called.

            You have to make the function call unambiguous in this case either by removing the default parameter on the second overload or examining why there are 2 overloads and removing one of them.

            Comment

            • SpecialKay
              New Member
              • Mar 2008
              • 109

              #7
              i cant imagin why you would ever need to do something like this.
              but you have your answer.

              Comment

              Working...