gpt4 book ai didi

c++ - 在对象上调用特定函数(函数指针)

转载 作者:行者123 更新时间:2023-11-30 01:28:01 26 4
gpt4 key购买 nike

如果我有一个函数指针

MyFunctionPointer myFunctionPointer;

我可以给它传递如下参数:

myFunctionPointer(1,2);

我想做类似的事情,但在对象上调用特定函数。我目前有这样的东西:

if(case1)
myObject.Case1();
else if(case2)
myObject.Case2();

相反,有没有办法做类似的事情:

  myObject.myFunctionPointer();

?

谢谢,

大卫

最佳答案

检查这个:http://www.goingware.com/tips/member-pointers.html .也许对您有帮助?

引用:

class Foo
{
public:
double One( long inVal );
double Two( long inVal );
};

int main()
{
double (Foo::*funcPtr)( long ) = &Foo::One;
Foo aFoo;
double result =(aFoo.*funcPtr)( 2 );
}

关于c++ - 在对象上调用特定函数(函数指针),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8087198/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com