'this' 指针在调用成员函数后发生变化 [英] 'this' pointer changes after calling a member function

查看:37
本文介绍了'this' 指针在调用成员函数后发生变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Visual Studio 2012 Express 调试 64 位应用程序.假设 'foo' 和 'bar' 都是类 C 的一些成员函数.

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' 看起来像:

'foo' looks like:

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

我的程序崩溃了,因为当它进入bar"时this"指针发生了变化.该问题可以通过更改为 'this->bar()' 来解决.

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

推荐答案

你的意思是下面的代码有效?

You mean that the following code works?

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

所以,假设 foo() 是一个成员函数,它为同一个对象调用另一个成员函数 bar(),检查两件事:(1) 是否有另一个具有相同成员函数签名的非成员函数 bar()bar(),如果存在,更改非成员函数名称 op 总是用 this-> 限定成员函数调用作为一种良好的编程习惯 (2) 当您为某个对象实例或指向对象实例的指针调用 foo() 时,检查对象是否已正确分配和初始化.

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.

这篇关于'this' 指针在调用成员函数后发生变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆