指向成员函数的函数指针
有几个重复项,但没有人解释为什么我可以使用成员变量来存储指针(在 FOO
中),但是当我尝试使用局部变量时(在 BAR 的注释部分中)
),这是非法的。有人能解释一下吗?
#include <iostream>
using namespace std;
class FOO
{
public:
int (FOO::*fptr)(int a, int b);
int add_stuff(int a, int b)
{
return a+b;
}
void call_adder(int a, int b)
{
fptr = &FOO::add_stuff;
cout<<(this->*fptr)(a,b)<<endl;
}
};
class BAR
{
public:
int add_stuff(int a, int b)
{
return a+b;
}
void call_adder(int a, int b)
{
//int (BAR::*fptr)(int a, int b);
//fptr = &BAR::add_stuff;
//cout<<(*fptr)(a,b)<<endl;
}
};
int main()
{
FOO test;
test.call_adder(10,20);
return 0;
}
There are several duplicates of this but nobody explains why I can use a member variable to store the pointer (in FOO
) but when I try it with a local variable (in the commented portion of BAR
), it's illegal. Could anybody explain this?
#include <iostream>
using namespace std;
class FOO
{
public:
int (FOO::*fptr)(int a, int b);
int add_stuff(int a, int b)
{
return a+b;
}
void call_adder(int a, int b)
{
fptr = &FOO::add_stuff;
cout<<(this->*fptr)(a,b)<<endl;
}
};
class BAR
{
public:
int add_stuff(int a, int b)
{
return a+b;
}
void call_adder(int a, int b)
{
//int (BAR::*fptr)(int a, int b);
//fptr = &BAR::add_stuff;
//cout<<(*fptr)(a,b)<<endl;
}
};
int main()
{
FOO test;
test.call_adder(10,20);
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
显然,您误解了
FOO
调用中this->*
的含义。当您将
this->*
与成员fptr
指针一起使用时,this->*
部分与fptr 完全无关>fptr
是FOO
的成员。当您使用指向成员的指针调用成员函数时,您必须使用->*
运算符(或.*
运算符),并且您总是 必须指定要与该成员指针一起使用的实际对象。这就是调用表达式的this->*
部分的作用。即,调用总是看起来像或作为
调用的左侧(
<指向对象的指针>
或换句话说,无论
fptr
是成员变量、局部变量、全局变量还是任何其他类型的变量,通过fptr
的调用都会总是看起来像是假设您想使用
*this
对象调用它。再举一个例子,如果您想为指针pfoo
指向的其他对象调用它,则调用将如下所示在您的
BAR
类中,调用应如下所示 < code>(this->*fptr)(a,b) 即使fptr
是局部变量。Apparently, you misunderstand the meaning of
this->*
in the call inFOO
.When you use
this->*
with the memberfptr
pointer, thethis->*
part has absolutely nothing to do withfptr
being a member ofFOO
. When you call a member function using a pointer-to-member, you have to use the->*
operator (or.*
operator) and you always have to specify the actual object you want to use with that pointer-to-member. This is what thethis->*
portion of the calling expression does. I.e. the call will always look asor as
The left-hand side of the call (
<pointer-to-object>
or<object>
above) cannot be omitted.In other words, it doesn't matter whether
fptr
is a member variable, local variable, global variable or any other kind of variable, the call throughfptr
will always look asassuming that you want to invoke it with
*this
object. If, for another example, you want to invoke it for some other object pointed by pointerpfoo
, the call will look as followsIn your
BAR
class the call should look as(this->*fptr)(a,b)
even thoughfptr
is a local variable.当使用成员函数指针时,需要指定它所作用的对象。
也就是说,您需要创建一个指向 BAR 实例的指针(我们称之为
bar
)并执行 do:来调用该函数,或者创建一个 BAR 实例并执行 do:
换句话说:
When you use a member function pointer, you need to specify the object on which it is acting.
I.e. you need to create a pointer to an instance of BAR (let's call it
bar
) and do:to call the function, or an instance of BAR and do:
Put another way:
我不认为变量的使用本身是非法的。尝试在没有类实例的情况下调用该方法是非法的。
也就是说,您应该真正调用
(someVar->*fptr)(a,b)
,其中someVar
的类型为BAR*
I don't think the usage of the variable itself is illegal. What's illegal is trying to call that method without a class instance.
That is, you should really call
(someVar->*fptr)(a,b)
wheresomeVar
is of typeBAR*
BAR::call_adder()
有几个问题。其一,你正在混合案例。在 C++ 中,大小写很重要。BAR
和bar
不同。其次,在修复大小写问题后,您很好地对指针进行了标记和分配,但是当您尝试通过指针调用成员函数时,您需要将operator ->*
与类对象一起使用。这是call_adder()
已修复BAR::call_adder()
had a couple of problems. For one, you were mixing case. In C++, case is signifigant.BAR
andbar
are not the same. Second, you decalred and assigned the pointer fine, after fixing the case problems, but when you try to call through the pointer to a member function, you need to useoperator ->*
with a class object. Here's iscall_adder()
fixed当您调用类的成员函数时,编译器会生成代码以在函数运行时设置“this”。当您从未完成的函数指针调用它时。有很多方法可以解决这个问题,但它们不能“保证”工作并且依赖于编译器。只要您小心并知道可能遇到的问题,您就可以做到。
When you invoke a member function of a class the compiler generates code to set 'this' while the function runs. When you call it from a function pointer that isn't done. There are ways to get around it but they aren't 'guaranteed' to work and are compiler dependent. You can do it as long as you're careful and know the possible problems you can run into.