Since your Speak(void *)
function takes an untyped/void pointer, it's unclear what sort of argument it is expecting. i.e. is Speak()
expecting its argument to be a pointer to a NUL-terminated byte array, or is it expecting its argument to be a pointer to a std::string
object? Since void *
is untyped, either one will compile, but passing in the "wrong" argument will likely cause a crash or other undefined behavior at runtime.
That said, if Speak(void *)
is implemented to expect its argument to point to a std::string
, then you could call it like this (as you do above):
Speak(&foregroundWindow);
or if Speak(void *)
is implemented to expect its argument to be a pointer to a NUL-terminated char array, you could instead call it like this:
Speak(const_cast<void *>(foregroundWindow.c_str()));
The best approach, though would be to modify the argument-type of the Speak()
method to specify the type of the pointer it takes, rather than accepting a void *
, so that the compiler could flag an error when the programmer passes in an inappropriate value, rather than allowing the programming error to pass undetected and cause a runtime malfunction. So something like:
void Speak(std::string * pointerToStdString);
or better yet (if Speak() doesn't need to modify the std::string
):
void Speak(const std::string * pointerToStdString);
or (if Speak() just needs the pointer-to-the-NUL-terminated-chars-array)
void Speak(const char * pointerToChars);