What is the output of the following C program?
[gamipress_button type=”submit” label=”Run Code Online” onclick=”window.open(‘https://coderseditor.com’,’_blank’)”]
class Test1 { int y; }; class Test2 { int x; Test1 t1; public: operator Test1() { return t1; } operator int() { return x; } }; void fun ( int x) { }; void fun ( Test1 t ) { }; int main() { Test2 t; fun(t); return 0; }
Studently Answered question October 11, 2022
Output: Compiler Error
The Test2 class defines two conversion operators. So the Test2 object can be automatically converted to both int and Test1. So the fun(t) function call is ambiguous. Since there are two functions void fun(int ) and void fun(Test1 ) the compiler has no way of deciding which function to call. In general, conversion operators can introduce ambiguity and should be overloaded with caution.
Studently Answered question October 11, 2022