GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 24-Mar-2006, 09:06
gemini_v440 gemini_v440 is offline
New Member
 
Join Date: Mar 2006
Posts: 4
gemini_v440 is on a distinguished road

difference between inline function and macro function


hi,
i would like to know difference between inline function and macro in c++. Given an option , which one should be used or given preference over the other???
  #2  
Old 25-Mar-2006, 21:11
alcoholic's Avatar
alcoholic alcoholic is offline
Member
 
Join Date: Nov 2005
Posts: 170
alcoholic is on a distinguished road

Re: difference between inline function and macro function


Inline functions anytime win over Macros..atleast It gives you a chance of writing a better code(though no guarantees). macros are simply evil most of the time.
__________________
Gravitation is not responsible for people falling in love.
-Albert Einstein
  #3  
Old 25-Mar-2006, 21:36
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough

Re: difference between inline function and macro function


I agree with alcoholic, but the OP also asked for an explanation of the difference.

Observe this code, which uses a macro function and an inline function for the same purpose:
CPP / C++ / C Code:
#define INC_X(x) (++(x))
inline void inc_x(int x) { ++x; }
inline void inc_x_ref(int &x) { ++x; }

int main()
{
      int y = 2;
      INC_X(y); //y is now 3
      inc_x(y); //y is still 3
      inc_x_ref(y); //y is now 4
      return 0;
}
The difference is that the INC_X line will be converted by the preprocessor (before the compiler ever sees it) to "++(y)". The preprocessor does direct text substitution. The inc_x line, on the other hand, will be preserved as a function call, and will be inlined by the compiler. But since inc_x doesn't return its variable, it is useless and its argument will not be incremented. The fix for this, of course, is to use a reference, which is what inc_x_ref does. I hope this useless and convoluted code sample helps you understand the difference.

By the way, the nature of macros creates a huge number of headaches and potential gotchas, which I won't go into here, but they more than justify alcoholic's statement. You can find many rants about this on Google.
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
determinant algorithms Blake C++ Forum 11 13-Mar-2006 19:36

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 01:30.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.