macro

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mar11
    New Member
    • Sep 2009
    • 38

    macro

    hi all,

    look at this function:
    Code:
    string test(string str)}{
     
    str.insert("my string");
    
    return str;
    }
    how can i implement it as a macro.

    thank for each support
  • Tassos Souris
    New Member
    • Aug 2008
    • 152

    #2
    A macro replaces a text for some other text. The text that is to be replaced is
    test and the replacement text is str.insert( "my string" ).
    Code:
    #define text_to_be_replaced( parameter ) replacement_text

    Comment

    • mar11
      New Member
      • Sep 2009
      • 38

      #3
      i do't know it dose not work with me...

      Code:
      #include <string>
      #include <iostream>
      
       string TEST1(string str){
        
       str.append("my string");
        
       return str;
       }
      
      int main(){
      string stri;
      stri = TEST1("this string is ");  cout << stri<<endl;
      }  // output -- this string is my string
      Now, how can i implement this function as macro and how will be returned..

      please can you explain it through executable example

      thanks for each support

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        What have you got so far for the macro?

        By the way, as an avid C programmer I'm very big on macros ... but I've heard rumblings that C++ experts strongly advise against using them. If this is a self-assigned task so you have flexibility, your C++ skills might be better served by creating an inline function.

        Comment

        • Tassos Souris
          New Member
          • Aug 2008
          • 152

          #5
          Assume this code example:
          Code:
          #define MAX_STRING_LEN 100
          
          char string[ MAX_STRING_LEN ];
          We define MAX_STRING_LEN to be 100. This means that whenever the
          text MAX_STRING_LEN is found it is replaced by 100. So, after the preprocessor step, the above code now becomes:
          Code:
          char string[ [B]100[/B] ];
          Now let's take a macro that takes an integer and increments it:
          Code:
          #define INT_INCR( integer ) ( ( integer )++ )
          
          int x = 0;
          INT_INCR( x );
          After the preprocessor step the above code becomes:
          Code:
          int x = 0;
          (x)++;
          Hope that helps. Show us what you have done so far.

          Comment

          • mar11
            New Member
            • Sep 2009
            • 38

            #6
            Dear Tassos,

            my main question is still not answerd to tell you what i have done.. So i realy confused about the bossibility of implementing the returned functions (like the example i have written) as a macro. I know i can implement it as inline functions and inline functions is better to interpret form compiler. but in many applications the macros are still used ...

            could you please focuse on this point?

            thank for each support...

            Comment

            • donbock
              Recognized Expert Top Contributor
              • Mar 2008
              • 2427

              #7
              mar11: Tassos and I are gently urging you to post your best effort first. We'll try to guide you from there.

              Comment

              • mar11
                New Member
                • Sep 2009
                • 38

                #8
                Also, this macro will be work okay
                Code:
                #define TEST( a, b )  (a = b+ 4)
                
                
                int c(0), d(0), e(0);
                e = TEST( c,d);
                cout << e << endl;
                but when i want to add a for statement, it does not work anymore??

                is there any bossibility to add for statment in the macro??

                thank a lot in advance
                Code:
                #define TEST( a, b ) for(int i=0; i<10; i++) (a = b+ i)
                
                
                int c(0), d(0), e(0);
                e = TEST( c,d);
                cout << e << endl;

                Comment

                • donbock
                  Recognized Expert Top Contributor
                  • Mar 2008
                  • 2427

                  #9
                  Originally posted by mar11
                  but when i want to add a for statement, it does not work anymore??
                  is there any bossibility to add for statment in the macro??
                  Code:
                  #define TEST( a, b ) for(int i=0; i<10; i++) (a = b+ i)
                  
                  int c(0), d(0), e(0);
                  e = TEST( c,d);
                  cout << e << endl;
                  This macro ought to expand to a proper for-loop. The problem is that you're trying to assign the value of the for-loop to e. For-loops don't have a value. You macro expands to this:
                  Code:
                  e = for(int i=0; i<10; i++) (c = d+i);

                  Comment

                  • mar11
                    New Member
                    • Sep 2009
                    • 38

                    #10
                    thanks, it works now..

                    Comment

                    • weaknessforcats
                      Recognized Expert Expert
                      • Mar 2007
                      • 9214

                      #11
                      Tassos: Do not use macros in C++.

                      1) you have no control over how the user exands the macro. You can get results that are inaccurate or just won't compile.
                      2) when you debug the code for the macro is not in your source file so your debugger cannot step through the macro.
                      3) that forces you to stop the build after the preprocessopr step and visually examine the macro expansion to see that it expanded to something that makes sense
                      4) etc...

                      Macros are really bad news.

                      Your example in C++ is solved by using a template.

                      Comment

                      • Tassos Souris
                        New Member
                        • Aug 2008
                        • 152

                        #12
                        Originally posted by weaknessforcats
                        Tassos: Do not use macros in C++.

                        1) you have no control over how the user exands the macro. You can get results that are inaccurate or just won't compile.
                        2) when you debug the code for the macro is not in your source file so your debugger cannot step through the macro.
                        3) that forces you to stop the build after the preprocessopr step and visually examine the macro expansion to see that it expanded to something that makes sense
                        4) etc...

                        Macros are really bad news.

                        Your example in C++ is solved by using a template.
                        No i am not using C++.... :)
                        And yes i agree with you... :)

                        Personally even in C (i use C not C++) i prefer enumerations over #defines and when it comes to macros with parameters i always provide a function version.

                        Comment

                        Working...