Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   function as a macro (http://www.go4expert.com/forums/function-macro-t23220/)

rahulonly4u 6Sep2010 11:13

function as a macro
 
Dear

i want to know can at what extent we can make a complex function as a macro



for ex.

getFromString(CString s, LPCTSTR delemiter)
{

CString temp;

int m= s.findof(delemiter);
if(m!=-1)
temp= s.mid(m);


}

LordN3mrod 7Sep2010 00:43

Re: function as a macro
 
Well, this particular one would be like this:


#define GET_FROM_STRING(s, delim) \
{\
CString temp;\

int m= s.findof(delemiter);\
if(m!=-1)\
temp= s.mid(m);\
}


Note the \ symbols - they concatenate physical source lines.
But making a macro out of such a funvtion is a horrible idea, because macros don't have scope, their parameters don't have types and, of course many surprising results can occur due to literal text substitution.

rahulonly4u 7Sep2010 17:15

Re: function as a macro
 
Thanks very much

rahulonly4u 7Sep2010 17:18

Re: function as a macro
 
Thanks again to LordN3mrod for this valuable tips i m going to do this mistake

LordN3mrod 7Sep2010 17:27

Re: function as a macro
 
I will repeat myself, DON"T use a macro unless you have to. In this particular case an inline function (a function delcared with the keyword inline, and defined in the same ".h" file) is a superior alternative.


All times are GMT +5.5. The time now is 18:02.