Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ void* to parameter to a function

I have this function in some library:

class myConsole
{
    void addCommand( std::string command, void* fn );
    ...
}

and in my class I have this function:

void myApp::TestFn( const std::vector<std::string> & args )
{
    // do something
}

in the same class I call this:

void myApp::initApp( )
{
    myConsole::getSingleton( ).addCommand( "FirstTest", &myApp::TestFn );
}

but this gives me this error:

error c2664 cannot convert parameter 2 from 'void(__thiscall myApp::*)(const std::vector<_Ty>&)' to 'void *'

how can I solve this?

thanks in advance!

like image 260
ghiboz Avatar asked Jun 03 '26 20:06

ghiboz


1 Answers

You can't solve this. You can't reliably cast a function pointer to void * and back.

(I suggest you redesign the program and stay clear of void*; there's no real need for it in C++.)

like image 180
Fred Foo Avatar answered Jun 06 '26 09:06

Fred Foo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!