1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

function as a macro

Discussion in 'C' started by rahulonly4u, Sep 6, 2010.

  1. rahulonly4u

    rahulonly4u New Member


    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);
    temp= s.mid(m);

  2. LordN3mrod

    LordN3mrod New Member

    Well, this particular one would be like this:

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

    int m= s.findof(delemiter);\
    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 likes this.
  3. rahulonly4u

    rahulonly4u New Member

    Thanks very much
  4. rahulonly4u

    rahulonly4u New Member

    Thanks again to LordN3mrod for this valuable tips i m going to do this mistake
  5. LordN3mrod

    LordN3mrod New 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 and shabbir like this.

Share This Page