Posted by: Zeeshan Amjad | April 1, 2020

Function Overload on Return type in C++


In C++ you can’t overload function on return type and there is a reason of it. Suppose C++ allow you to overload function on return type and we overload one function on the basis of return type in such a way that one function return integer and other return character. And when you try to assign return value in character variable then the character return type function is called and when the value is being assigned in integer variable then integer version is called and in gives error when try to assign it in any other type of variable. But which function will be executed when we call the function without assign its value in any variable? Like this

	// which function will be called?
	Fun();

There is ambiguity in this function call and compiler can’t decide which function should be called. Due to this reason function overloading on the basis of return type is not allowed in C++

But it seems attractive that you decide the function calling on the basis of return type and do processing on the basis of the variable in which you are going to assign functions return value. In fact you can simulate this behavior with the help of conversion function which is also called Operator function. Conversion function is basically used for conversion from one type to another.

class Fun
{
public:
	operator int() { std::cout << "int" << std::endl; return 0; }
	operator char() { std::cout << "char" << std::endl; return ' ';  }
	operator float() { std::cout << "float" << std::endl; return 0.0; }
};

And the calling of these conversion operators is just like calling the functions. And it seems that functions are overloaded on the basis of return type.

	// call int function
	int i = Fun();
	// call char function
	char c = Fun();
	// call float function
	float f = Fun();

To be very honest this is in fact not function overloading on return type, it is just a simulation of function overloading on return type. The syntax which seems function calling is in fact making the unmade temporary object of Fun class and calling its default constructor. You can do the same thing with the help of named object. And this example clearly shows in fact what is going in the above code.

	// making object of Fun class
	Fun fun;
	// call int function
	int i = fun;
	// call char function
	char c = fun;
	// call float function
	float f = fun;

No matter you accept it as a true function overloading on return type or not, it may be useful technique for building and extending libraries. And Of course eye opener for your friends when you ask him to do so at least first time.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Categories

%d bloggers like this: