Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

sflare

how to get classname?

This topic is 5646 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i would like to beable to get the name of the class in the function code from the class automatically (c++). Anyone know how? (if it''s possible) example: class MYCLASS { void my_function() { printf("%s",GET_CURRENT_CLASSNAME); <<<<---- HERE } }; where GET_CURRENT_CLASSNAME would give something like "MYCLASS" it would be something like the MACRO __LINE__ i think i remember reading other ones for function names.... Thanks

Share this post


Link to post
Share on other sites
Advertisement
I was just quickly reading about

UnDecorateSymbolName()
it seems using UNDNAME_NAME_ONLY may help for this problem

also would need
BOOL SymGetSymFromAddr(
HANDLE hProcess,
DWORD Address,
PDWORD Displacement,
PIMAGEHLP_SYMBOL Symbol
);

to get the decorated name in the first place, right?

Share this post


Link to post
Share on other sites
If there is one for function names, I don''t know about it. But you could write a macro yourself:


std::string g_sFuncName;
#define FUNC( func ) func { g_sFuncName = #func;

...

HRESULT FUNC( Fap::Foo( int bar, float baz ) )
// do stuff
printf( "%s", g_sFuncName );
}


It''s not pretty (it kills the open brace ''{'') but it''ll work.

I like pie.

Share this post


Link to post
Share on other sites
UndecorateSymbolName and all that stuff won''t help here. That''s for undoing the symbol name mangling done by C++ compilers when retrieving a symbol from a DLL.

Can you clarify what you want exactly? Do you want a macro that will generate the class name automatically at compile time, maybe for debugging purposes? Or do you want to be able to check an object''s class at runtime? (like Java''s instanceof operator, if you''re familiar with Java)

If it''s the first one, I don''t think there''s an easy way to do that in C++, possibly not any way at all. If what you really want is the second option, there are ways. You need to enable RTTI and use typeid or dynamic_cast. Google with some combination of keywords: C++ RTTI dynamic_cast typeid

Share this post


Link to post
Share on other sites
I might be saying something stupid, but... why not write the class name yourself? It''s not prone to change too much anyway, or if it is going to, simply #define it beforehand?

Like :


class MYCLASS
{
void my_function()
{
printf("%s", "MYCLASS");
}
};

//or :


#define CLASSNAME MYCLASS
#define sCLASSNAME "MYCLASS"

class CLASSNAME
{
void my_function()
{
printf("%s",sCLASSNAME);
}
};



ToohrVyk

Share this post


Link to post
Share on other sites
quote:
Original post by sflare
i would like to beable to get the name of the class in the function code from the class automatically (c++).
Anyone know how? (if it''s possible)

Strictly speaking, its not possible. Once a C++ program is compiled, much useful information is removed in the name of efficiency, including most meta-information about the types within your program. Some meta-information is retained for RTTI purposes. If you wish to retain information beyond what RTTI provides, you must build your own Meta-Object Protocol. Perhaps you should describe the problem you think requires this as a solution, and someone will point you in the direction of prior art for solving the problem.

Share this post


Link to post
Share on other sites
Actually, it is possible.

There's an operator called "typeid" which will return a
"const type_info&"-Object of the "type_info"-Class.

The "type_info"-Class has a member-function
'const char* name() const' which will give
you the class name.

typeid [msdn online]
type_info [msdn online]
[both links only work in IE sadly]

edit:
quote:

From MSDN:
Type information is generated for polymorphic classes only if the /GR (Enable Run-Time Type Information) compiler option is specified.


Without those is seems to be working fine without RTTI even.

[edited by - Wildfire on July 2, 2003 9:23:30 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by Wildfire
Actually, it is possible.

There's an operator called "typeid" which will return a "const type_info&"-Object of the "type_info"-Class.

The "type_info"-Class has a member-function 'const char* name() const' which will give you the class name.

It gives you *a* name, which is not guaranteed to be *the* name. As you acknowledge, the class also needs to be polymorphic, which isn't the case with the OP's example. Depending on the actual problem, those details might be a non-issue, which is why I asked what the problem is.

[edited by - SabreMan on July 2, 2003 9:29:42 AM]

Share this post


Link to post
Share on other sites
quote:

As you acknowledge, the class also needs to be polymorphic



No, I did not say that. My quote from MSDN says that you need to enable RTTI if you want to use typeid with polymorphic classes. Otherwise you need not enable RTTI (All of this applies to VisualC++, but since it says 'C++ specific' it should be valid for other compilers too)

And, it does give you the name of the class, not just any name. I've used it myself, even works with STL/Templates!
For example:


std::vector<char> charVector;

std::cout << typeid(charVector).name();


will result in "class std::vector <char,class std::allocator <char> >" as output .

edit: freaking pointy brackets...

[edited by - Wildfire on July 2, 2003 9:52:52 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by Wildfire
And, it does give you the name of the class, not just any name. I''ve used it myself


That isn''t guaranteed by the standard. What you''re describing is microsoft specific behaviour. Yes, it''s pedantic but yes it''s true. And pedantry gets you a long way when trying to write portable code.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!