-1

The win32 debugger shows the class name in question in the inspector window that means the information must be available, it's only in question wether the microsoft developers made the function public.

For Debug/Diagnostic/development purpose i wanna write a function to get the instanciated class name of a c++ object as an output and as an input the pointer to the base class.

For example:

const char* GetNameOfInstanciatedClassByBaseClassPtr(class B* pBaseClassOrInstanciatedClass);

class B { public: virtual void Init (){
  assert(StringIsEqual(GetNameOfInstanciatedClassByBaseClassPtr(this), "C")); } };
class C : B {} c; c.Init(); 

So please provide a solution for implementation of function GetNameOfInstanciatedClassByBaseClassPtr.

So question is: Whats the required lib/header/function ? I guess a API for the win32 Debug info would help?

Writing virtual functions which return strings or type_info is not an acceptable solution in my situation.

As i said before: this is for diagnostic/debug/development purpose only, NOT for production code so please avoid discussions about clean code purposes. I could also collect the information manually but since it's about 260 classes i might be faster this way. ;)

EDIT: In the first version of the question i had the function 'GetNameOfInstanciatedClassByBaseClassPtr' called in the constructor of the base class but i observed the information is not yet available in the constructor time. So i put it to an Init method which is always called. In this method the debugger shows the name of the instanciated class so if i put call to the function GetNameOfInstanciatedClassByBaseClassPtr into my init method the information can be obtained. Same seems to be true for the rtti-typeinfo.

EDIT2: As mentioned it may not work for void*, you may need a class B*. However, this can be easily tested. Note some things may be compiler specific. So i adapted the question again.

So the answer i took from the comments is to use either

  • Runtime Type Information (rtti) (through native c++) [1]
  • Microsoft Debug Interface Access SDK (through COM) [2]

[1] You need to enable this feature in your compiler settings for file or project.

[2] That is the more complex solution.

seebee
  • 1
  • 3
  • *"The win32 debugger shows the class name in question in the inspector window"* from a `void*` or from a `B*`? – Jarod42 Jul 29 '21 at 08:03
  • 1
    debugger use pdb file. take pointer to virtual table. look which symbol name at this address. parse name. etc. all this very not simply – RbMm Jul 29 '21 at 08:32
  • 1
    This information gets emitted by the linker into a program database (PDB) file. You can use the [Debug Interface Access SDK](https://learn.microsoft.com/en-us/visualstudio/debugger/debug-interface-access/debug-interface-access-sdk) to query the information from there. – IInspectable Jul 29 '21 at 08:40
  • @Jarod42: Good point, it was definitely not the void*, it wsa a class*. Also i think the typeinfo may work in my case, of course not inside the constructor, at a point where the child class is not initialized... – seebee Aug 10 '21 at 09:23
  • @IInspectable: Very valueable, thank you. That was my first approach after i reputed found, that typeinfo does not work. but as i said it could not work in the base class constructor for the child class in c++ i think. i think it can be done with the debug interface access sdk and also the typeinfo at least inside a function called after the object has been completely constructed. – seebee Aug 10 '21 at 09:29

1 Answers1

0

You can use typeid:

#include <iostream>
#include <typeinfo>

struct A {
    virtual void printName(){
        std::cout << typeid(*this).name() << "\n";
    }
    virtual ~A() = default;
};
struct C : A{};

int main() {
    C c;
    c.printName();
    A a;
    a.printName();
}

However, the name is implementation defined. Possible output is:

1C
1A

And you cannot get the typeid of the dynamic type of the object from a void*. See this somewhat related question: How to get the typeid of a void* pointer?

Also (from cppreference:

If typeid is used on an object under construction or destruction (in a destructor or in a constructor, including constructor's initializer list or default member initializers), then the std::type_info object referred to by this typeid represents the class that is being constructed or destroyed even if it is not the most-derived class.

That is, you cannot use typeid to infer the dynamic type in the constructor (or destructor). The only way I am aware of to get your hands on the dynamic type in a constructor or destructor is a dynamic_cast, but then you need to know the type beforehand.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • Thanks. I tried this and it just returned the name of the base class. I enabled the rtti compiler option, to make it work like this. is there an extra option in win32 to make it return the child class name? – seebee Jul 29 '21 at 08:17
  • @seebee not sure what you mean. `1C` is the name of `C` – 463035818_is_not_an_ai Jul 29 '21 at 08:17
  • Ye but you said depending of the implementation (of the compiler?!) it may also return 1A (name of A). In my case this happened but this doesn't solve the problem since i need the name of the "sealed" / "instanciated" class "C" / "1C". I'm asking wether microsoft implemented a compiler switch to control this behaviour (Return 1A or 1C) – seebee Jul 29 '21 at 08:31
  • @seebee the names are implementation defined per class. Ie output could also be `CCC AAA`. Different classes have different names – 463035818_is_not_an_ai Jul 29 '21 at 08:36
  • of course it is not in question the resulting class name will represent the actual identifier used in the code. the question is, wether the information for the child class is available in the base class constructor. and this seems to fail, for rtti and for debug info. so also for a class ChildClass the result will be "BaseClass" if you call the GetTypeInfo in the constructor of class BaseClass. That is different to c# i think. – seebee Aug 10 '21 at 10:01
  • @seebee in constructor ans destructors the dynamic type does not matter. In constr/dest the type is always die type your currenlty constructir / destructing – 463035818_is_not_an_ai Aug 10 '21 at 15:05
  • @seebee see edit. It isnt possible in the constructor – 463035818_is_not_an_ai Aug 10 '21 at 15:42
  • yes exactly. thats the point it isnt possible in the *baseclass* contructor. actually i was a bit out of focus with c++ since i did c# a very long time. i remember better now how the memory model works in c++ and now it seems obvious to me that it does not work in the base ctor. – seebee Oct 05 '22 at 12:45