0

Is there a way (without overloading) to call a function like this

GetValue("one");
//AND
std::string test("one");
GetValue(test);

The function is simply:

int GetValue(const std::string &MyString)
	{
		return MyMap[MyString];
	}

This overload does the job:

int GetValue(const char* MyString)
	{
		return MyMap[MyString];
	}

But that seems a little annoying to have to do. Any suggestions?

Thanks
Dave

2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by daviddoria
0

The overload is not needed. As long as the function parameter is a const reference to string, you can pass a string constant and it will become a string object. Libraries usually have the overload to avoid the cost of constructing a new string object, but it is not required.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.