Home > Backend Development > C++ > How to Get the Current Class Name in C ?

How to Get the Current Class Name in C ?

Patricia Arquette
Release: 2024-11-15 18:25:03
Original
342 people have browsed it

How to Get the Current Class Name in C  ?

Macro CLASS Equivalent in C

In C , unlike the FUNCTION macro that provides the name of the current function, there is no built-in macro that directly provides the name of the current class.

Solution

As mentioned in the provided answer, a similar functionality can be achieved using the typeid(*this).name() method. However, this method has limitations when used in static methods.

For static methods and general scenarios, alternative approaches using macros are available. One approach involves utilizing the PRETTY_FUNCTION macro:

For Method Name:

#define __METHOD_NAME__ methodName(__PRETTY_FUNCTION__)

inline std::string methodName(const std::string& prettyFunction) {
    // Extract and return the method name
    ...
}
Copy after login

For Class Name:

#define __CLASS_NAME__ className(__PRETTY_FUNCTION__)

inline std::string className(const std::string& prettyFunction) {
    // Extract and return the class name
    ...
}
Copy after login

Note that this approach relies on the PRETTY_FUNCTION macro, which is specific to certain compilers like gcc.

The above is the detailed content of How to Get the Current Class Name in C ?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template