语句完全被忽略(好像:被编译器省略?)

发布于 2024-10-03 11:35:35 字数 935 浏览 0 评论 0原文

好吧,我再次碰上了砖头.. 又是我不明白它错误的原因,也不知道如何让它工作:

我有一个 Human 类的实例。 Human 类派生自 ObjectObject 类有一个称为“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忘羡 2024-10-10 11:35:35

该宏在此上下文中不执行任何操作,它只是扩展为指向函数的指针。要实际调用该函数,您需要一对额外的括号。

myObj->myfunc;   // does nothing
myObj->myFunc(); // calls the function 

我希望编译器已经对此进行了优化,因此没有代码可以执行或中断。您可以通过以下方式清楚地看到这一点:

  • 首先不使用宏(首选),
  • 使用编译器的选项来输出与相应汇编器交错的源代码。

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.

myObj->myfunc;   // does nothing
myObj->myFunc(); // calls the function 

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:

  • not using the macro in the first place (preferred)
  • using your compiler's option to output source code interleaved with corresponding assembler.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文