简体   繁体   English

指向类方法的C ++指针

[英]C++ pointer to class method

I want to do something like 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 that I need something similar to CLI::*handler, but I can't get the syntax right. 我知道我需要类似于CLI :: * handler的东西,但语法不正确。 I keep running into errors like this: 我一直遇到这样的错误:

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

This illustrates the correct 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 ) { return (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 member functions do not decay to pointer-to-member. 成员函数的名称不会衰减到成员指针。 You must use & explicitly, and a qualified name, when creating a pointer-to-member. 创建成员指针时,必须显式使用&以及限定名称。

In addition to other answers, another option is to use std::function together with std::bind() : 除了其他答案,另一种选择是将std::functionstd::bind()一起使用:

struct CLI_Command{
    ...
    std::function<void> handler;
};

class CLI {
    ...
    CLI_Command cli_table[NO_CLI_COMMANDS] = {
      { "Command1",   std::bind(&CLI::handler1, this) },
      { "Command2",   std::bind(&CLI::handler2, this) }
    };

    void handler1(){};
    void handler2(){};
};
void handler1(){}
void handler2(){}

are member functions of CLI . CLI成员函数。 The correct way to "address to" them is &CLI::handler1 and not handler1 . “寻址”它们的正确方法是&CLI::handler1而不是handler1 However then, they won't be accepted by void (*h)(void) , which would need to be changed to void (CLI::*h)(void) . 但是,它们将不会被void (*h)(void) ,需要将其更改为void (CLI::*h)(void) But that is probably not what you want. 但这可能不是您想要的。

Maybe consider reading about std::function for type erasure, or make your handler1/handler2 static. 也许考虑阅读有关类型擦除的std::function ,或使您的handler1/handler2静态。

You should use the syntax for a pointer to class member instead of the syntax for a loose function pointer. 您应该对指向类成员的指针使用语法,而不对松散的函数指针使用语法。

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);
};

In addition, make sure you call the function through the pointer of the current CLI class; 另外,请确保您通过当前CLI类的指针来调用该函数;

void CLI::process(char *cmd) {
    CLI_Command command* = /* lookup the command */
    this->(command->handle)();
}

To get it working, make your methods static 要使其正常工作,请将您的方法static

static void handler1(){};
static void handler2(){};

Whatever consequences ( read here please, for more detailed info) this will have :-( . 不管有什么后果( 请在此处阅读 ,以获取更多详细信息),它将产生:-(。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM