debugger: How to get address of class name in constructor

0

NOTE: I'm not trying to solve a programming problem; I'm trying to learn.

Given this c++ code:

#include <iostream>

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

        }   // breakpoint
};

int main()
{
    class1 c1;
}

Using VS2019; breakpoint set per above. In Immediate Window:

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

Also, in Memory window, entering "class1" in Address field shows: Unable to evaluate the expression.

At the breakpoint, my questions:

  1. How can I get the address of class1 in the immediate window by using the identifier "class1"? (yes, I know it's the same address as "this")
  2. Same question re: Memory window
  3. Why does "class1" in Immediate Window return blank?

enter image description here

c++
visual-studio
debugging
immediate-window
asked on Stack Overflow Sep 6, 2020 by SAbboushi

1 Answer

0

I am guessing that you wish to access the RTTI of the class. Chances are, different compilers may implement it slightly differently to one another.

That said, there is an operator called typeid: https://en.cppreference.com/w/cpp/language/typeid

Take a look at this blog for an example of using it with the Visual-C compiler: https://blog.quarkslab.com/visual-c-rtti-inspection.html

which features this example:

#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;
}

which outputs:

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

If you're interested in accessing the debug information in the PDB file (MSVC) then take a look at the answers to this question: Do debugging information reveal code in C++ / MSVC?

answered on Stack Overflow Sep 6, 2020 by Den-Jason • edited Sep 6, 2020 by Den-Jason

User contributions licensed under CC BY-SA 3.0