CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Sep 2004
    Location
    A Planet Called Earth... :-)
    Posts
    835

    overloaded function resolution...

    There are 3 overloaded functions.
    I would like to know why second is being called here, (on my compiler the second is being called).

    Code:
     #include<iostream> 
    using namespace std;
    
    int func(short &a)
    {
     return a + 1;
    }
    
    int func(int &a)
    {
     return a + 2;
    }
    
    int func(long &a)
    {
     return a + 3;
    }
    
    
    int main()
    {
     int num = 0;
     std::cout << func(num) << std::endl;
    }
    Thanks in advance.

    Regards
    Shaq
    C++ program ran... C++ program crashed... C++ programmer quit !!

    Regards

    Shaq

  2. #2
    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    762

    Re: overloaded function resolution...

    You sent an int to a function and the function that ran was the one that accepted an int, correct?

    Where is the confusion? Or did I miss something?

  3. #3
    Join Date
    Sep 2004
    Location
    A Planet Called Earth... :-)
    Posts
    835

    Re: overloaded function resolution...

    Quote Originally Posted by kasracer
    You sent an int to a function and the function that ran was the one that accepted an int, correct?

    Where is the confusion? Or did I miss something?
    Fine.
    Lets, change the code a little, to...
    (remove reference parameter, lets use copies of the parameter passed)
    Code:
    #include<iostream>
     
    using std::cout;
    using std::endl;
     
    int func(short a)
    {
    	return a + 1;
    }
     
    int func(int a)
    {
    	return a + 2;
    }
     
    int func(long a)
    {
    	return a + 3;
    }
     
     
    int main()
    {
    	cout << func(0) << endl;
    }

    I hope this dosent go deep into overloaded function resolution.
    Last edited by Vedam Shashank; January 16th, 2006 at 03:55 AM.
    C++ program ran... C++ program crashed... C++ programmer quit !!

    Regards

    Shaq

  4. #4
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: overloaded function resolution...

    Quote Originally Posted by Vedam Shashank
    I hope this dosent go deep into overloaded function resolution.
    What is the problem that you are facing? This also resolves as expected. Regards.

  5. #5
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: overloaded function resolution...

    This resolves as expected, because 0 is an int. To call other functions either use a long integer constant (0l) or explicitly cast the integer constant ((short)0).

  6. #6
    Join Date
    Sep 2004
    Location
    A Planet Called Earth... :-)
    Posts
    835

    Re: overloaded function resolution...

    Does that mean, if i have a constant declared within range of int..., by default, it will be an integer, and not a short or a float.
    What happens if long is greater than int (in terms of size), and i declare an constant that is greater than int range. Can i assume that the function which accepts long gets called.
    C++ program ran... C++ program crashed... C++ programmer quit !!

    Regards

    Shaq

  7. #7
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: overloaded function resolution...

    Quote Originally Posted by Vedam Shashank
    Does that mean, if i have a constant declared within range of int..., by default, it will be an integer, and not a short or a float.
    It will not be a short. Floats always have a decimal point (e.g. 0.0, so they cannot be confused.
    Quote Originally Posted by Vedam Shashank
    What happens if long is greater than int (in terms of size), and i declare an constant that is greater than int range.
    Some compilers will automatically declare this as a long constant, others will not accept it, unless you provide the long or unsigned long suffix. Probably some will give you undefined behavior by simply using a constant that fits in the range.

  8. #8
    Join Date
    Jun 2002
    Location
    Moscow, Russia.
    Posts
    2,176

    Re: overloaded function resolution...

    In most cases overloading isn't beneficial. It may introduce unnecessary confusion when it's not evident which function is getting called. Though modern IDEs simplify matters somewhat.
    "Programs must be written for people to read, and only incidentally for machines to execute."

  9. #9
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: overloaded function resolution...

    The section(s) in the standard covering overload resolution are among the most complex in there. I'm still struggling to understand all the nuances.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  10. #10
    Join Date
    Sep 2004
    Location
    A Planet Called Earth... :-)
    Posts
    835

    Re: overloaded function resolution...

    I was asked this in an interview (a written one).

    I was wondering if it would be good to have 3 such functions defined, in an actual project. I would try to avoid such, for i feel it depends so much on a programmers knowledge.

    As for this...
    Quote Originally Posted by Graham
    The section(s) in the standard covering overload resolution are among the most complex in there. I'm still struggling to understand all the nuances.
    That sounds like bad news to ones who wants to dig deep into overload resolution.
    C++ program ran... C++ program crashed... C++ programmer quit !!

    Regards

    Shaq

  11. #11
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: overloaded function resolution...

    Quote Originally Posted by Vedam Shashank
    I was wondering if it would be good to have 3 such functions defined, in an actual project.
    ...Yeah, atleast I haven't come under such a situation where I would need to provide overloads with short, int and long.

    By the way, you have been giving a lot of interviews lately .. all the best with those .

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured