简体   繁体   English

指向类方法调用的C ++指针

[英]C++ pointer to a class method call

In my class Tabla I have a public pointer to method: 在我的类Tabla中,我有一个指向方法的公共指针:

public:
    int (Tabla :: *punterofunc)(int,int);

In main I point it to a class method: 我主要将其指向类方法:

tablita.punterofunc = &Tabla :: in_lineal;

But this call doesn't work! 但是这个电话不起作用!

 tablita->punterofunc(num,0);

I think you're looking for this tasty syntax: 我认为您正在寻找这种美味的语法:

((tablita).*(tablita.punterofunc))(num,0);

tablita.punterofunc is a member function pointer. tablita.punterofunc是成员函数指针。 The general syntax for calling a pointer-to-member-function p on an object o is: 在对象o上调用指向成员函数的指针p的一般语法为:

((o).*(p))(args...);

Just apply that to your code. 只需将其应用于您的代码即可。 (Some of the parens might not be necessary in all cases (not sure), but if you stick with that, it should work all the time.) (不一定会在所有情况下都不需要某些括号(不确定),但是如果坚持下去,它应该一直有效。)

尝试这个:

tablita.*punterofunc(num,0);

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

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