Thread: Function Overloading Dilemma

  1. #1
    Registered User
    Join Date
    Oct 2023
    Posts
    18

    Function Overloading Dilemma

    Code:
    void utils::calc(int a)  { /* different code */ }
    void utils::calc(char b) { /* different code */ }
    Which function is called for calc(3) ?

    Also refer to :
    Function overloading in C++ - Stack Overflow

  2. #2
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    277
    Quote Originally Posted by upload View Post
    Code:
    void utils::calc(int a)  { /* different code */ }
    void utils::calc(char b) { /* different code */ }
    Which function is called for calc(3) ?

    Also refer to :
    Function overloading in C++ - Stack Overflow
    Implicit conversions from char to int are just part of the language. C++ inherited that from C where functions such as getchar() return an int (not a char). As such the best thing to do is avoid those kinds of overloads altogether.

  3. #3
    Registered User
    Join Date
    Oct 2023
    Posts
    18
    Sure a char can be promoted to a 32-bit int, but if the argument was a floating-point type and not a char, information would be lost.
    But the idea is, how to determine which function is used ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function overloading
    By Nikosant03 in forum C++ Programming
    Replies: 5
    Last Post: 01-29-2021, 06:17 AM
  2. Virtual function polymorphism dilemma.
    By Chris87 in forum C++ Programming
    Replies: 4
    Last Post: 05-15-2011, 02:40 AM
  3. Dilemma with "operator overloading"...
    By Petike in forum C++ Programming
    Replies: 3
    Last Post: 08-24-2008, 03:33 PM
  4. A little on Function Overloading
    By zbap in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2002, 02:22 PM
  5. Overloading a function like this
    By Koedoe in forum C++ Programming
    Replies: 8
    Last Post: 05-13-2002, 07:09 AM

Tags for this Thread