C++ pointer to class method -



C++ pointer to class method -

i want this:

struct cli_command{ cli_command(char* s, void (*h)(void)){ command_string = s; handler = h; } char* command_string; void (*handler)(void); }; class cli { public: cli(); private: cli_command cli_table[no_cli_commands] = { cli_command("command1", handler1), cli_command("command2", handler2) }; void handler1(){}; void handler2(){}; };

i know need similar cli::*handler, can't syntax right. maintain running errors this:

"error: no matching function phone call 'cli_command::cli_command(const char [4], <unresolved overloaded function type>)"

this illustrates right syntax:

class cli; struct cli_command { cli_command(char* s, void (cli::*h)(void)) { command_string = s; handler = h; } char* command_string; void (cli::*handler)(void); void raise( cli* the_cli ) { homecoming (the_cli->*handler)(); } }; class cli { public: cli(); private: static cli_command cli_table[no_cli_commands]; void handler1(){}; void handler2(){}; }; cli::cli_command cli_table[no_cli_commands] = { { "command1", &cli::handler1 }, { "command2", &cli::handler2 } };

names of fellow member functions not decay pointer-to-member. must utilize & explicitly, , qualified name, when creating pointer-to-member.

c++ class methods function-pointers

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -