簡體   English   中英

調試器:如何在構造函數中獲取類名的地址

[英]debugger: How to get address of class name in constructor

注意:我不是要解決編程問題; 我正在嘗試學習。

鑒於此 C++ 代碼:

#include <iostream>

class class1
{
    public:
        int attr1 = 12;
        class1()
        {

        }   // breakpoint
};

int main()
{
    class1 c1;
}

使用VS2019; 上面設置的斷點。 在立即窗口中:

this
0x000000877f54fcf4 {attr1=0x0000000c }
    attr1: 0x0000000c

attr1
0x0000000c

class1::attr1
0x0000000c

&(class1::attr1)    (am able to use "class1" here, I assume, because it's clear I'm not referring to a typename)
0x000000877f54fcf4 {0x0000000c}

class1        (returns blank line?)


&class1       (treats class1 as type)
type name is not allowed

此外,在內存窗口中,在Address字段中輸入“class1”顯示:無法計算表達式。

在斷點處,我的問題是:

  1. 如何使用標識符“class1”在立即窗口中獲取class1的地址? (是的,我知道它與“this”地址相同)
  2. 同樣的問題:內存窗口
  3. 為什么立即窗口中的“class1”返回空白?

在此處輸入圖片說明

我猜你想訪問班級的 RTTI。 有可能,不同的編譯器可能會以稍微不同的方式實現它。

也就是說,有一個名為typeid的運算符: https : //en.cppreference.com/w/cpp/language/typeid

看看這個博客的一個例子,它與 Visual-C 編譯器一起使用: https : //blog.quarkslab.com/visual-c-rtti-inspection.html

具有此示例的功能:

#include <iostream>
class MyClass {};
class MyDerivedClass : public MyClass {};

template<typename T> void PrintTypeInformation(char const* pVarName, T const& t)
{
  std::cout
    << pVarName
    << ": type: "     << typeid(t).name()
    << ", raw type: " << typeid(t).raw_name()
    << std::endl;
}

int main(void)
{
  MyClass cls;
  MyDerivedClass *drv;
  int n;
  char c;
  __int64 l;
  double d;

#define PRINT_TYPE_INFORMATION(var) PrintTypeInformation(#var, var)

  PRINT_TYPE_INFORMATION(cls);
  PRINT_TYPE_INFORMATION(drv);
  PRINT_TYPE_INFORMATION(n);
  PRINT_TYPE_INFORMATION(c);
  PRINT_TYPE_INFORMATION(&l);
  PRINT_TYPE_INFORMATION(d);

  return 0;
}

輸出:

cls: type: class MyClass, raw type: .?AVMyClass@@
drv: type: class MyDerivedClass *, raw type: .PAVMyDerivedClass@@
n: type: int, raw type: .H
c: type: char, raw type: .D
&l: type: __int64 *, raw type: .PA_J
d: type: double, raw type: .N

如果您對訪問 PDB 文件 (MSVC) 中的調試信息感興趣,請查看以下問題的答案: Do debugging information display code in C++ / MSVC?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM