我想知道是否可以将其他结构的函数指向结构:
Example:
typedef struct
{
int func(int z)
{
return z * 2;
}
} sta;
typedef struct
{
int(*this.func)(int);
} stah;
int main()
{
sta sa;
stah sah;
sah.func = &sa.func;
return 0;
}
这可能在结构中吗?
的声明func
应该看起来像这样:
int(sta::*func)(int);
或者,另一种选择:
using my_type = int(sta::*)(int);
my_type func;
这更容易阅读:my_type
是类型的别名指向成员函数的指针sta
得到一个int
并返回一个int
.
func
只不过是具有类型的数据成员my_type
.
为了将实际的成员函数指针分配给func
,你可以这样做:
sah.func = &sta::func;
然后您可以按如下方式调用它:
(sa.*sah.func)(0);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)