Find the answer to your Linux question:
Results 1 to 3 of 3
In one of my interview i faced a different question on function overloading. Code: void call(int a, int b){ } int call(int a, float b){ return 1;} The above functions ...
  1. #1
    Just Joined!
    Join Date
    May 2009
    Posts
    21

    reg: C++ function overloading

    In one of my interview i faced a different question on function overloading.

    Code:
    void call(int a, int b){ }
    int call(int a, float b){ return 1;}
    The above functions will compile and works fine. But,
    
    void call(int a, int b){}
    int   call(int a, int b){}
    This will gives error.
    So, why compiler is not taking the return type into consideration for checking the function overloading.

    Thanks in advance,
    Madhu
    Last edited by MikeTbob; 01-15-2011 at 10:15 PM. Reason: Added Code Tags

  2. #2
    Linux Newbie
    Join Date
    Mar 2010
    Posts
    121
    Quote Originally Posted by madhuti View Post
    So, why compiler is not taking the return type into consideration for checking the function overloading.
    Quite simply it's because the compiler can't reliably differentiate between function calls based solely on return-type. For example, with the above two function definitions, a call like this:

    Code:
    int x = call(0, 1);
    obviously wants the second function, as the first one doesn't supply a return value, but one like this:

    Code:
    call(0, 1);
    is ambiguous - you could be meaning to call either function for their side-effects alone.

  3. #3
    Just Joined!
    Join Date
    Jan 2011
    Posts
    1

    Post function overloading and template

    this website contains complete source code and tutorial on function overloading and templates in C++:
    bitsbyta dot blogspot dot com

Posting Permissions

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