What is need of default arguments in function overloading in C++?

Recommended Answers

All 2 Replies

Is this what you are trying to do?
This is a very general example

#include <iostream>
#include <string>

using namespace std;

string myReturn( string in )
{
	return in;
}

int myReturn( int in )
{
	return in;
}

int main()
{
	string strIn = "20";
	int intIn = 20;
	cout << myReturn(strIn) << endl;
	cout << myReturn(intIn) << endl;
	
	system("PAUSE");
	return 0;
}

There is no need that I know of. There is an option, just like there is with any function, whether it is overloaded or not. I don't think that having one function with default parameter and one with same argument that isn't default will count as overloading as the compiler won't know which function to use.

int add(int, int);
int add(int, double); //valid overload of int as parameters are different
int add(int, double = 44.44); //I don't think this one is legal if second one exists.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.