Difference between macro and pass by reference?

This is a discussion on Difference between macro and pass by reference? within the C++ Programming forums, part of the General Programming Boards category; Like the title says, what is the difference between using a macro, which just textually substitutes whatever, as opposed to ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    19

    Difference between macro and pass by reference?

    Like the title says, what is the difference between using a macro, which just textually substitutes whatever, as opposed to just writing a function with reference parameters? I know there must be a difference, or else there would be no need for macros. Can someone please help me to understand the difference, possibly with an example? TIA.

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    129
    Macros shouldn't be used as functions - it's a C-style to make something inline. It's a bit harder to debug and off use due the inlines in C++.

    Code:
    Macro
    #define foo(x) x+  // see the error
    ...
    int main()
    {
       int y;
       foo(y);  // This is the line you get the error
       char s[] = "Hello world";
       foo(s);  // Want to know what happens?
       ...
    
    
    inline
    inline void foo(const int &x)
    {
      x+;  //  Now you'll get the error here
    }
    
    int main()
    {
       int x;
       foo(x);
       char s[] ="dlrow olleH";
       foo(s);  //  And another one here
       ...
    kooma - t.h@iki.fi

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    Inline functions also enforce type safety.........

    Also #defines may never actually reach the compiler as they are converted by the preprocessor beofre compilation.....I have read of occasions where this can give problems debugging....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculation of sizof through macro. Possible?
    By freeindy in forum C Programming
    Replies: 4
    Last Post: 09-24-2007, 03:05 PM
  2. macro function
    By bradleyd in forum C Programming
    Replies: 8
    Last Post: 05-21-2007, 05:18 PM
  3. Passing Keys (macro program)
    By Coder87C in forum C Programming
    Replies: 3
    Last Post: 07-08-2003, 06:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21