Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. create Predefined Macros

create Predefined Macros

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mmwhw
    wrote on last edited by
    #1

    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

    M J 2 Replies Last reply
    0
    • M mmwhw

      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

      M Offline
      M Offline
      Mike Nordell
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • M mmwhw

        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

        J Offline
        J Offline
        Jambolo
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups