开发者

Statements completely ignored (as if: omitted by compiler?)

开发者 https://www.devze.com 2023-01-26 04:09 出处:网络
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:

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,开发者_StackOverflow社区&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?


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.
0

精彩评论

暂无评论...
验证码 换一张
取 消