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. question about macro define

question about macro define

Scheduled Pinned Locked Moved C / C++ / MFC
question
5 Posts 4 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.
  • H Offline
    H Offline
    HeartFriend
    wrote on last edited by
    #1

    void func(UINT nOperation) { #if (nOperation == OPERATION_1) #define OPERATION(spObj, nParam) fun1(spObj, nParam) #elif (nOperation== OPERATION_2) #define OPERATION(spObj, nParam) func2(spObj, nParam) #elif (nOperation== OPERATION_3) #define OPERATION(spObj, nParam) func3(spObj, nParam) nFrameNo) #else #define OPERATION(spObj, nParam) #endif .... OPERATION(spObj, n) // whatever nOperation be, it always call fun1,why??? }

    J S 2 Replies Last reply
    0
    • H HeartFriend

      void func(UINT nOperation) { #if (nOperation == OPERATION_1) #define OPERATION(spObj, nParam) fun1(spObj, nParam) #elif (nOperation== OPERATION_2) #define OPERATION(spObj, nParam) func2(spObj, nParam) #elif (nOperation== OPERATION_3) #define OPERATION(spObj, nParam) func3(spObj, nParam) nFrameNo) #else #define OPERATION(spObj, nParam) #endif .... OPERATION(spObj, n) // whatever nOperation be, it always call fun1,why??? }

      J Offline
      J Offline
      Jose Lamas Rios
      wrote on last edited by
      #2

      Macros don't work at runtime; they are processed before compilation. Try something like this instead:

      void func (UINT nOperation)
      {
         switch (nOperation)
         {
            // [edited - Thanks DavidCrow] case default:
            default:
               break;
       
            case OPERATION_1:
               fun1(spObj, nParam);
               break;
       
            case OPERATION_2:
               fun2(spObj, nParam);
               break;
       
            case OPERATION_3:
               fun3(spObj, nParam);
               break;
         }
      }

      -- jlr http://jlamas.blogspot.com/[^]

      D 1 Reply Last reply
      0
      • H HeartFriend

        void func(UINT nOperation) { #if (nOperation == OPERATION_1) #define OPERATION(spObj, nParam) fun1(spObj, nParam) #elif (nOperation== OPERATION_2) #define OPERATION(spObj, nParam) func2(spObj, nParam) #elif (nOperation== OPERATION_3) #define OPERATION(spObj, nParam) func3(spObj, nParam) nFrameNo) #else #define OPERATION(spObj, nParam) #endif .... OPERATION(spObj, n) // whatever nOperation be, it always call fun1,why??? }

        S Offline
        S Offline
        S Senthil Kumar
        wrote on last edited by
        #3

        :omg: You've mixed up precompiler directives with code. Macro expansion (and execution of other precompiler directives) happen before code is compiled. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

        1 Reply Last reply
        0
        • J Jose Lamas Rios

          Macros don't work at runtime; they are processed before compilation. Try something like this instead:

          void func (UINT nOperation)
          {
             switch (nOperation)
             {
                // [edited - Thanks DavidCrow] case default:
                default:
                   break;
           
                case OPERATION_1:
                   fun1(spObj, nParam);
                   break;
           
                case OPERATION_2:
                   fun2(spObj, nParam);
                   break;
           
                case OPERATION_3:
                   fun3(spObj, nParam);
                   break;
             }
          }

          -- jlr http://jlamas.blogspot.com/[^]

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          Jose Lamas Rios wrote: case default: No case needed here.


          "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

          J 1 Reply Last reply
          0
          • D David Crow

            Jose Lamas Rios wrote: case default: No case needed here.


            "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

            J Offline
            J Offline
            Jose Lamas Rios
            wrote on last edited by
            #5

            DavidCrow wrote: Jose Lamas Rios wrote: case default: No case needed here. Yep, you are right. -- jlr http://jlamas.blogspot.com/[^]

            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