C++ 获取此->的函数指针;c1->;c2->;c3->;myFunc();
获取通过指针访问的函数的指针时遇到问题:C++ 获取此->的函数指针;c1->;c2->;c3->;myFunc();,c++,function-pointers,C++,Function Pointers,获取通过指针访问的函数的指针时遇到问题: double *d = &(this->c1->...->myFunc(); 不起作用,myFunc()被声明为double。 有什么方法可以做到这一点吗?如果您的意思是想要一个指向myFunc返回的值的指针,那么您不能:它是临时的,将在表达式末尾被销毁 如果需要指针,则还需要一个非临时值来指向: double value = this->c1->...->myFunc(); double * d = &am
double *d = &(this->c1->...->myFunc();
不起作用,myFunc()
被声明为double
。
有什么方法可以做到这一点吗?如果您的意思是想要一个指向
myFunc
返回的值的指针,那么您不能:它是临时的,将在表达式末尾被销毁
如果需要指针,则还需要一个非临时值来指向:
double value = this->c1->...->myFunc();
double * d = &value;
或者你是说你想要一个指向函数的指针?这与双精度*的类型不同:
// get a member-function pointer like this
double (SomeClass::*d)() = &SomeClass::myFunc;
// call it like this
double value = (this->c1->...->*d)();
或者你是说你想要像一个简单函数一样可以调用的东西,但是绑定到某个对象this->c1->…
?该语言不直接支持这一点,但C++11有lambdas和一个用于这类事情的bind
函数:
// Bind a function to some arguments like this
auto d = std::bind(&SomeClass::myFunc, this->c1->...);
// Or use a lambda to capture the object to call the member function on
auto d = [](){return this->c1->...->myFunc();};
// call it like this
double value = d();
如果您的意思是想要一个指向由
myFunc
返回的值的指针,那么您不能:它是临时的,将在表达式末尾被销毁
如果需要指针,则还需要一个非临时值来指向:
double value = this->c1->...->myFunc();
double * d = &value;
或者你是说你想要一个指向函数的指针?这与双精度*的类型不同:
// get a member-function pointer like this
double (SomeClass::*d)() = &SomeClass::myFunc;
// call it like this
double value = (this->c1->...->*d)();
或者你是说你想要像一个简单函数一样可以调用的东西,但是绑定到某个对象this->c1->…
?该语言不直接支持这一点,但C++11有lambdas和一个用于这类事情的bind
函数:
// Bind a function to some arguments like this
auto d = std::bind(&SomeClass::myFunc, this->c1->...);
// Or use a lambda to capture the object to call the member function on
auto d = [](){return this->c1->...->myFunc();};
// call it like this
double value = d();
假设在
this->c1->c2->c3->myFunc()
c3的类型为foo
:
class foo
{
public:
double myFunc();
};
然后你可以说:
typedef double (foo::*pmyfunc)(void);
然后记下它的地址:
pmyfunc addr = &foo::myFunc;
您应该阅读函数常见问题。假设在
this->c1->c2->c3->myFunc()
c3的类型为foo
:
class foo
{
public:
double myFunc();
};
然后你可以说:
typedef double (foo::*pmyfunc)(void);
然后记下它的地址:
pmyfunc addr = &foo::myFunc;
您应该阅读函数常见问题解答。是否需要指向函数或其结果的指针?是否需要指向函数或其结果的指针?谢谢您的帮助!在第一种情况下,我得到一个错误:&expected L value…(翻译自德语)我试图做的是有一个指向function@user1679802:对不起,我复制/粘贴错误。我现在已经修好了。谢谢你的帮助!在第一种情况下,我得到一个错误:&expected L value…(翻译自德语)我试图做的是有一个指向function@user1679802:对不起,我复制/粘贴错误。我现在已经修好了。