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

Recommended Answers

All 2 Replies

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.

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.