'this' pointer changes after calling a member function

纵然是瞬间 提交于 2019-12-13 05:17:08

问题


I use Visual Studio 2012 Express to debug a 64-bit app. Let's say both 'foo' and 'bar' are some member functions of a class C.

'foo' looks like:

void foo() {
    bar();    // change to this->bar() works!
}

My program crashed because 'this' pointer is changed when it went inside 'bar'. The problem can be fixed by changing to 'this->bar()'.

Any idea how I should debug this problem? Thanks


回答1:


You mean that the following code works?

void foo() {
    this->bar();    // instead of bar()
}

So, presuming foo() is a member function that calls another member function bar() for the same object, check two things: (1) whether there is another non-member function bar() having same signature of member function bar(), if so exist, change the non-member function name op always qualify with this-> for member function call as a good programming practice (2) When you call foo() for some object instance or pointer to object instance, check whether the object has been allocated and initialized properly.



来源:https://stackoverflow.com/questions/26172249/this-pointer-changes-after-calling-a-member-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!