Connect two third party modules with "const char*" and "char*" arguments

If and only if the function called via _BASS_PluginLoad doesn't alter the memory pointed at by file, you can use a const_cast:

HPLUGIN temp = _BASS_PluginLoad(const_cast<char*>(strPlugName),0);

Some old c API's are not const correct on account of the const keyword being a fairly late addition to the C language. But they still don't mutate their arguments, so a const_cast is the easiest way to make use of them in const correct C++ wrappers. It's a perfectly legitimate reason (maybe even the reason) for that cast.


The easy and safe way is to copy the argument into a local buffer, and then pass a pointer to that. As you are using C++, you can automate the memory management.

bool loadLibrary(const char *strPlugName){
  std::string local(strPlugName);
  local.push_back('\0'); // Ensure null terminated, if not using C++11 or greater
  HPLUGIN temp = _BASS_PluginLoad(&local[0],0);
  return false;
}

If using C++17, you can just call local.data() instead of &local[0].

Language lawyer caveat:

Strictly speaking, &local[0] was not defined to work in C++98 - in practice it always did (and later versions of the standard defined it to work).

Tags:

C++