create Predefined Macros
-
All know that the vc has some Predefined Macros,such as __LINE__ ,__FILE__ etc.when the programmer write the code __FILE__ in the file myclass.cpp ,the compiler will convert it to "myclass.cpp". Now i want to create a macros __CLASS__,so when programmer write the code __CLASS__ in a class (such as CMyClass )function ,the compile will convert it to the class Name ("CMyClass"),can i realize that ?if can ,how? CMyClass { void DisplayMyName(); } void CMyClass::DisPlayMyName() { AfxMessageBox(__CLASS__);//When Compile it will be converted to AfxMessageBox("CMyClass") } thanks
-
All know that the vc has some Predefined Macros,such as __LINE__ ,__FILE__ etc.when the programmer write the code __FILE__ in the file myclass.cpp ,the compiler will convert it to "myclass.cpp". Now i want to create a macros __CLASS__,so when programmer write the code __CLASS__ in a class (such as CMyClass )function ,the compile will convert it to the class Name ("CMyClass"),can i realize that ?if can ,how? CMyClass { void DisplayMyName(); } void CMyClass::DisPlayMyName() { AfxMessageBox(__CLASS__);//When Compile it will be converted to AfxMessageBox("CMyClass") } thanks
You can't. Meditate upon the meaning of "predefined". Even if you didn't have the "predefined" constraint, what you request can't be done without modifying the translator (compiler). You can "work around" this by enforcing conventions, much like e.g. MFC does for its "runtime class" thingie, but there's nothing within the C++ language to help you out here.
-
All know that the vc has some Predefined Macros,such as __LINE__ ,__FILE__ etc.when the programmer write the code __FILE__ in the file myclass.cpp ,the compiler will convert it to "myclass.cpp". Now i want to create a macros __CLASS__,so when programmer write the code __CLASS__ in a class (such as CMyClass )function ,the compile will convert it to the class Name ("CMyClass"),can i realize that ?if can ,how? CMyClass { void DisplayMyName(); } void CMyClass::DisPlayMyName() { AfxMessageBox(__CLASS__);//When Compile it will be converted to AfxMessageBox("CMyClass") } thanks
You can do is something like this:
class CMyClass
{
static char const __CLASS__[];
...
void DisplayMyName();
}...
char const CMyClass::__CLASS__[] = "CMyClass";However in this case, __CLASS__ is not a macro and the value is not available at compile time. You can't do compile-time string concatenation.