T
T
tema862016-10-20 11:15:27
C++ / C#
tema86, 2016-10-20 11:15:27

Is the return type of a function taken into account when deciding which version of an overloaded function to use?

Hello, I am reading Herbert Schildt's Beginner's Guide.
There is such an example:

#include <iostream>
using namespace std;

void f(int x);
void f(short x);
void f(double x);

int main() {
        int i = 10;
        double d = 10.1;
        short s = 99;
        float r = 11.5F;

        f(i);
        f(d);
        f(s);
        f(r);

        return 0;
}

void f(int x) {
        cout << "В функции f(int): " << x << "\n";
}

void f(short x) {
        cout << "В функции f(short): " << x << "\n";
}

void f(double x) {
        cout << "В функции f(double): " << x << "\n";
}

Results:
In the function f(int): 10
In the function f(double): 10.1
In the function f(short): 99
In the function f(double): 11.5
At the end of the chapter there is a knowledge test and there is such a question:
Is the type of value taken into account, returned by the function, in the decision to choose the right version of the overloaded function?
Below is the answer to it:
No, the types of values ​​returned by functions may differ, but this difference does not affect the decision to choose the right version.
--------------------
Actually, I had a question why the data type is not taken into account to select the version of the overloaded function, when, after casting to double from float, the function f is selected (double x)?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Armenian Radio, 2016-10-20
@tema86

C++ has implicit type casting. What function to call if exactly the same prototype does not exist?

float foo() { ... };
double foo(){ ... };

int bar()
{
    int k=foo(); ///?????
    ...
}

No answer. Therefore, the focus will fail.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question