I am trying to make my C++ code cross platform. On Windows I am using the Windows.h header file and on macOS and Linux I use the unistd.h header file.
I am calling a function if the OS is windows which takes a char* type. The equivalent call for mac and linux takes char**. I currently have a function that takes a void* type so that I can have 1 function for both cases. I then check the operating system and reinterpret_cast to the correct type.
void DoSomething(void* thing) {
#if defined(__APPLE__) || defined(__linux__)
// Init thing as char**
thing = new char*[...];
thing[0] = new char[...];
.
.
.
UnixFunction(reinterpret_cast<char**>(thing));
#elif defined(_WIN32)
// Init thing as char*
thing = new char[...];
WindowsFunction(reinterpret_cast<char*>(thing));
#endif
}
int main() {
void* thing;
DoSomething(thing);
return 0;
}
Regardless if I should do this, is this safe or is this undefined behavior?