Home >Backend Development >C++ >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 ... }
For Class Name:
#define __CLASS_NAME__ className(__PRETTY_FUNCTION__) inline std::string className(const std::string& prettyFunction) { // Extract and return the class name ... }
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!