有没有一种方法可以引用集合中类的方法?

时间:2019-08-05 03:30:39

标签: c++ qt methods function-pointers class-members

我个人希望使用Qt,因此,如果代码关闭,请随时进行修复。

我想要的是创建一个可以引用类成员的集合。我尝试了函数指针,但出现错误,我不能使用非静态成员或类似的东西。我尝试了&ClassName::memberName,但这给了我一些关于它必须是常量还是某些东西的错误。

我不确定要提供哪种代码,因为我试图询问在c ++中是否可以实现某些功能。


上下文

由于人们可能会问我为什么要这样做,因此我计划在我的软件中列出一整套用于各种“工具”的方法。每个工具与4个鼠标事件相关:1个用于移动,释放,按下,双击。

我想保持效率,所以我希望做

enum class MouseAction { MOVE=-1, RELEASE, PRESS, DOUBLECLICK }
void mousePressed(MouseEvent *e) {mouseActionMethod[toolName][MouseAction::PRESS](e);}

为此,我需要一个map<Action, map<MouseAction, void*>>类的集合,在其中可以将类成员作为函数指针插入void*中。但是,这样做会导致错误。

如前所述,尝试以几种方式执行此操作,这令人困惑。如果我发现在浪费任何额外精力之前是不可能的,那就更好了。

根据我在堆栈溢出中发现的另一个问题,我也用void*替换了(ClassName*),尝试&ClassName::methodName –解决方案似乎并不适用给我。

2 个答案:

答案 0 :(得分:2)

您可以将函数指针用于常规函数或静态类方法:

void foo(int) {/**/}
struct S
{
    static void bar(int) {/**/}
};

// and then

main()
{
    void (*f1)(int) = &foo;
    void (*f2)(int) = &S::bar;

    f1(42); // calls foo(42)
    f2(42); // calls S::bar(42)
}

方法指针可用于非静态类方法

struct S
{
    void bar(int) {/**/}
};

// and then

main()
{
    void (S::*m)(int) = &S::bar;

    S s;

    (s.*m)(42); // call s.bar(42);
}

std::function是另一种选择:

struct S
{
    void bar(int) {/**/}
};

// and then

main()
{
    std::function<void (S*, int)> f1 = &S::bar;
    S s;
    std::function<void (int)> f2 = [&s](int i) { s.bar(i); }
    std::function<void ()> f3 = [&s]() { s.bar(43); }

    f1(&s, 42);
    f2(42);
    f3();
}

答案 1 :(得分:1)

import { Component } from '@angular/core'; import { NavController } from 'ionic-angular'; //Import the Http library and add it to the constructor. //Import the Constants provider and add it to the constructor. import { Http } from '@angular/http'; import { ConstantsProvider } from '../../providers/constants/constants'; //Import the post page import { PostPage } from '../post/post'; @Component({ selector: 'page-home', templateUrl: 'home.html' }) export class HomePage { //Create a property for the title of the blog. //Create a property for the list of blog posts. title: string; posts: any[]; constructor(public navCtrl: NavController, public constants: ConstantsProvider, public http: Http) { http.get('https://www.googleapis.com/blogger/v3/blogs/byurl?key=' + constants.getApiKey() + '&url=' + constants.getUrl()) .subscribe(response => { let data = response.json(); this.title = data.name; this.getPosts(data.posts.selfLink); }); } //Create a getPosts() function getPosts(url:string) { this.http.get(url+'?key='+this.constants.getApiKey()) .subscribe(response => { let data = response.json(); console.log(data); this.posts = data.items; }); } //Create and openPost(post) function to open the post page and pass the selected post as a parameter. openPost(post) { this.navCtrl.push(PostPage, {post:post}) } } 不是容纳函数指针的正确类型。

非静态成员类型除了其签名外,也绑定到该类类型。在以下示例中,指向void *的指针的类型为foo。当然,您可以有一个集合,例如void (A::*)(int)来保存这样的指针:QMap

QMap<QString, void (A::*)(int)> collection;

另外,为了简化操作,您可以给该类型起一个名字:

struct A {
    void foo(int a);
};

struct A { using MethodPointer = void (A::*)(int); void foo(int a); };


但是静态成员函数就像普通函数一样:

QMap<QString, A::MethodPointer> collection;

此处指向struct A { static void foo(int a); }; 的指针的类型为foo