function as a macro

rahulonly4u's Avatar, Join Date: Sep 2010
Go4Expert Member
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);


}
0
LordN3mrod's Avatar, Join Date: Sep 2010
Go4Expert Member
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.
shabbir like this
0
rahulonly4u's Avatar, Join Date: Sep 2010
Go4Expert Member
Thanks very much
0
rahulonly4u's Avatar, Join Date: Sep 2010
Go4Expert Member
Thanks again to LordN3mrod for this valuable tips i m going to do this mistake
0
LordN3mrod's Avatar, Join Date: Sep 2010
Go4Expert Member
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.
rahulonly4u, shabbir likes this