0

Okay I'm super confused because I read my class notes and listened to the lecture.

If all of the following functions exist in a C++ program and are prototyped first, which does

TestIt('A', int('A')) call?

A. int TestIt(char ch, char ix);

B. int TestIt(char ch, short ix);

C. int TestIt(char ch = 'A');

D. implementation dependent

E. none

Is this a trick question?

TestIt('A', 'A');

would return char char right?

Is it B because short is a type of int?

e.g. int('A')

It might be none as well.

2
  • "Is it B because short is a type of int?" short, int and char (as well as some others) all are integral types, or integer types. Commented Nov 1, 2013 at 23:32
  • 1
    It's not polymorphism Commented Nov 1, 2013 at 23:45

1 Answer 1

1

None, because it's ambiguous.

For TestIt('A', int('A')), the arguments are of type char and int. Two arguments mean that C) is not a viable candidate. A) and B) are viable.

However, the second argument has to be converted from int to either short or char. Both are integral conversions. Therefore, both have the same rank when comparing the overloads. Two or more functions with the same rank for a given set of arguments (and no tie-breaker applies) => ambiguous.

It would be different if you called TestIt('A', 'A') and the candidates were

  1. void TestIt(char, int)
  2. void TestIt(char, short)

You can try to find out as an exercise why it isn't ambiguous in this case ;)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.