如何从另一个类调用指向成员的函数?

时间:2018-03-09 22:21:08

标签: c++ class function-pointers member-function-pointers

我有两个班级,ButtonGame。 Game类包含Button *类型的成员,Button类包含指向Game类中成员函数的指针。

class Game{
    public:
        Button* button1;

        void init();
        void tick();

        void wantetEvent();
};

class Button{
    public:
        void (Game::*click)();
};

我现在在Game的click函数中定义init函数指针。

void Game::init(){
    button1 = new Button;
    button1->click = &Game::wantetEvent;
}

tick函数中,我想调用click的{​​{1}}函数。这是我到目前为止所尝试的内容:

button1

我尝试了很多其他的东西,但它们没有这些例子那么有希望。 问题是编译器在第一个示例中告诉我void Game::tick(){ (button1->*click)(); (button1->*Game::click)(); } ,在第二个示例中告诉我'click' was not declared in this scope。问题是我正在尝试访问一个函数 - 从指针的角度来看 - 是在另一个类中吗?或者是否有一些解决方案允许我以这种方式调用函数?

1 个答案:

答案 0 :(得分:2)

您需要一个实例来调用它:

services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = "Cookies";
            })
           .AddCookie("Cookies", options =>
           {
               options.LoginPath = new PathString("/Development/Account/Login");
               options.AccessDeniedPath = new PathString("/Development/Account/Login");
               options.LogoutPath = new PathString("/Development/Account/Logout");
               options.ExpireTimeSpan = TimeSpan.FromDays(1);
           });

        services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddIdentityServerAuthentication(options =>
            {
                var configAuthority = container.GetInstance<IOptions<CoPilotConfig>>().Value?.IdentityAuthority;
                options.Authority = configAuthority ?? $"http://localhost:{Constants.LOOPBACK_PORT}";
                options.RequireHttpsMetadata = false;
                options.ApiName = "CoPilotApi";
            });

Demo