Skip to main content
GameDev.net gamedev.net
🔒 Locked

[C++] retrieving typeinfo

Started by Wh0p Aug 30, 2011 at 1:29 PM 8 replies 1.3k views
Original Post
Wh0p
Wh0p
Hi,

I wanted to implement a simple class for my framework that keeps track of Objects that are instatiated, that i can put out some information at the end of the execution while debugging like this.
Most important for me is to know the variable name.

The Question: Is there a way to retrieve a string containing the debugging symbols the compiler generates?

I'd like to get that string ( if it is possible ) in the constructor like this:

MyObj::MyObj()
{
char* varname = DebugSymbolVarName(this);
...
}


I fear there's no function like DebugSymbolVarNam, but i think it clarifies what i want to achieve
Ectara
Ectara

Hi,

I wanted to implement a simple class for my framework that keeps track of Objects that are instatiated, that i can put out some information at the end of the execution while debugging like this.
Most important for me is to know the variable name.

The Question: Is there a way to retrieve a string containing the debugging symbols the compiler generates?

I'd like to get that string ( if it is possible ) in the constructor like this:

MyObj::MyObj()
{
char* varname = DebugSymbolVarName(this);
...
}


I fear there's no function like DebugSymbolVarNam, but i think it clarifies what i want to achieve


I haven't used C++ in years, but the first thing that comes to mind is to inherit from a base debug class with a virtual function to get the name of the class, and provide your own function for each class that implements this.
Wh0p
Wh0p
na that would just give me the typename, but i want the name of the variable so basically:

...
MyObj obj;
...


In such a peace of code i want to store the informaion that an object with the variable name >obj< has been created.
The best case wouble be if i could do that in the MyObj::MyObj() method.
rip-off
rip-off
Variables can have no names, or multiple names.

In the code below, what are the variable names:

class Example
{
// Constructor that saves "variable name"
};

int main()
{
Example(); // Anonymous value?

Example *a = new Example(); // Example() is called before "a" exists

Example *b = a; // Multiple pointers pointing to one value?

Example &r = *a; // Aliases?
}

Also, in most code bases I'd expect to get a lots of variables named "example" for a class called "Example".

The real question is, how do you expect to use this information? How are you going to introspect all these variables "at the end of the execution " - do you keep them all in some massive list?
Ectara
Ectara

Variables can have no names, or multiple names.

-snip-

The real question is, how do you expect to use this information? How are you going to introspect all these variables "at the end of the execution " - do you keep them all in some massive list?


Understanding the question now, I agree with the above. What is stopping you from using a debugger, or if absolutely necessary, using unique numerical ID's?
Adam_42
Adam_42
A quick search turned up this which suggest the portable option of typeid(*this).name() or some compiler specific macros, all of which will require a bit of work if you want an unmangled class name, but should be ok for debugging.
Wh0p
Wh0p
Ok,
makes sense I did not consider those cases...

The main idea behind all of this was to keep track of some objects and let me know if something is not setup or shutdown the right way. The "when", "where" and "what" should just help to identify the critical code section.
rip-off
rip-off

The main idea behind all of this was to keep track of some objects and let me know if something is not setup or shutdown the right way. The "when", "where" and "what" should just help to identify the critical code section.
[/quote]
A basic logging system should help there. You can include the __FILE__ and __LINE__ numbers, which will you quickly track down most problems.
SiCrane
SiCrane
If you want to identify when and where an object is created you could store a partial or full stack trace as part of construction.
Wh0p
Wh0p

A basic logging system should help there. You can include the __FILE__ and __LINE__ numbers, which will you quickly track down most problems.


The Logging system allready exists. Using __FILE__, __LINE__, __FUNCTION__ in the constructor will allways log the function name of the of the constructor etc.
so either i call them from the outside what i do not like
or ill use some makro workaround for the construction what i like even less

anyway thanks for your fast replies, they kind of made me think over the whole thing again

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.