UPDATE:
I went for the template approach cause it seemed the most elegant / concise but then I end up with stuff like this:
template<typename charType>
int doSomethingWithString(const charType* buffer)
{
wchar_t* tempBuffer = NULL;
if(typeid(charType) == typeid(char))
{
tempBuffer = (wchar_t*)malloc(sizeof(wchar_t)*strlen((char*)buffer));
mbstowcs(tempBuffer, (const char*)buffer, strlen((const char*)buffer));
}
else if(typeid(charType) == typeid(wchar_t))
{ tempBuffer = (wchar_t*)malloc(sizeof(wchar_t)*strlen((char*)buffer));
tempBuffer = wcscpy(tempBuffer, (const wchar_t*)buffer);
}
At which point I feel it’s kind of ugly (specially since I still have to have all those casts there to let the compiler know). I also tried turning the parameter into a wstring but I just don’t seem to find an appropriate constructor for both cases?
It’s been a while now that I’ve been away from C++ and I can’t remember the C++ way of going about this:
Let’s say I have a class with some method myClass::doSomethingWithString(…) and I want the same method to be able to be called passing in a character sequence as either const char* or const wchar_t*. Was there a way to make the method be able to accept both types of “strings” and tell if the parameter is const char* or const wchar_t* inside the method implementation? Or is method overloading the only way to go about it?
Method overloading is the only way to go about it.
Alternately, You could write a single template function.However, in that case the actions you perform inside the function would have to be same in either case. In this case the compiler will generate source code of the functions for your both the types.