c++ - gdb 在单例的静态方法中没有显示 '' 这个''?

标签 c++ debugging gdb debug-symbols

我有以下情况。这是该类的精简版。我在 Qt creator 中运行它并在真实场景中使用 Qt。

class MyClass
    {
    public:
         MyClass();
         static MyClass *instance;
         static void myMethod(int a, int b);
         int name;
    };

    MyClass  *MyClass::instance = 0;

    MyClass::MyClass(){
    if (instance)
        exit(-1);
    instance = this;
    }

void MyClass::myMethod(int a, int b){
        if(instance->name == a) qDebug() << "hello";
    }

int main(int argc, char *argv[])
{
    MyClass cls;
    cls.myMethod(1,2);
}

我正在尝试通过使用调试器进入它来调试 myMethod。当我进入该方法时,只有 a 1b 2 在 watch 中可见,并且没有对 this 的引用实例

更新 答案表明静态方法未绑定(bind)到对象,这就是为什么没有可用的 this 的原因。

在此实现中,静态方法访问 instance,这就是我希望在进入 myMethod 后在调试器中可用的内容。

我如何使它可用/可见?

最佳答案

静态方法实际上是在没有对象的情况下调用的。呼唤

MyClass cls;
cls.myMethod(1,2)

相当于

MyClass::myMethod(1, 1)

因此 myMethod 没有收到任何 this 值。

关于c++ - gdb 在单例的静态方法中没有显示 '' 这个''?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21776307/

相关文章:

c++ - 抑制警告 : possible loss of data - caused by truncation through casting 的单个实例

c++ - 在静态初始化程序中使用 getenv() 是否安全,即在 main() 之前?

c# - 使用 Visual Studio 2010 远程调试 C# .dll - 获取的 .PDB 文件不匹配 - 但它绝对是来自构建

在 Chrome 中调用的 Javascript 函数

xcode - "Environment Variable is too long"在 Xcode 4

c++ - 从 C++ 中的字符串中删除字符

c++ - 使用批处理文件使用 Cmake 和 Msbuild (vs2010) 单击一次构建我的项目

php - X调试错误: "Failed loading xdebug.so: xdebug.so: cannot open shared object file: No such file or directory"

ios - XCode4 调试器总是在 Main 中中断

c++ - 有没有办法使用纯 gdb 脚本来测试我们是否在断点上?