语句完全被忽略(好像:被编译器省略?)
好吧,我再次碰上了砖头.. 又是我不明白它错误的原因,也不知道如何让它工作:
我有一个 Human 类的实例。 Human 类派生自 Object。Object 类有一个称为“PerformStep”的虚拟函数。 Human 类重载了该函数。 Human 类还具有函数“WalkAction”。现在我想在 PerformStep 期间通过成员函数指针调用此“walkaction”。
我喜欢通过指针来做到这一点:
人类是愚蠢的:他们知道如何走路,但不知道何时。因此,在该步骤中将询问上帝实例:“我现在应该做什么” - 然后该上帝实例返回指向正确成员函数的指针。
virtual void PerformStep()
{
postion.x += 0; //redundant line to check for the debugger
CALL_MEMBER_FN(*this,&Human::WalkAction);
Object::PerformStep();
}
Human::WalkAction:
void WalkAction(){
position.x += 1;
}
CALL_MEMBER_FN(宏):CALL_MEMBER_FN(object,ptrToMember) ((object).*(ptrToMember))
问题是,当我在调试模式下运行程序时,它完全忽略带有函数指针。 - 如果我逐条运行它,它会“跳过”该行,如果我在步行操作中放置断点,它永远不会到达断点。如果我在特定行上放置断点,断点就会被推到下一个线。
这里发生了什么?
Well I've hit the brick once again.. Again something I don't see the reason it is wrong and have no idea how to make it work:
I have an instance of class Human. The class Human is derived from Object.. The Object class has a vitrual function called "PerformStep". The Human Class overloads this function.
Human class also has the function "WalkAction". Now I want to call this "walkaction" during the PerformStep - By member function pointer.
I like to do this by pointer as:
Humans are dumb: they know how to walk, but not when. So a god-instance would be asked during the step: "what shall I do now" - And then that god instance returns the pointer to the correct member function.
virtual void PerformStep()
{
postion.x += 0; //redundant line to check for the debugger
CALL_MEMBER_FN(*this,&Human::WalkAction);
Object::PerformStep();
}
Human::WalkAction:
void WalkAction(){
position.x += 1;
}
CALL_MEMBER_FN (macro):CALL_MEMBER_FN(object,ptrToMember) ((object).*(ptrToMember))
The problem is that when I run the program in debug mode, it completely ignores the line with the function pointer. - If I run it statement-by-statement it "jumps" over the line, if I put a breakpoint in walk action it never reaches the breakpoint.. If I put a breakpoint on the specific line, the breakpoints gets shoved to the next line.
What is happening here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该宏在此上下文中不执行任何操作,它只是扩展为指向函数的指针。要实际调用该函数,您需要一对额外的括号。
我希望编译器已经对此进行了优化,因此没有代码可以执行或中断。您可以通过以下方式清楚地看到这一点:
That macro does nothing in this context, it just expands to a pointer to function. To actually call the function, you need an extra pair of parentheses.
I expect the compiler has optimized this out, so there's no code to be executed or to break on. You could see this clearly by: