1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

function as a macro

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

  1. rahulonly4u

    rahulonly4u New Member

    Joined:
    Sep 6, 2010
    Messages:
    29
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    software developer
    Location:
    india
    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);


    }
     
  2. LordN3mrod

    LordN3mrod New Member

    Joined:
    Sep 4, 2010
    Messages:
    22
    Likes Received:
    11
    Trophy Points:
    0
    Occupation:
    Software Developer
    Location:
    Yerevan, Armenia
    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 likes this.
  3. rahulonly4u

    rahulonly4u New Member

    Joined:
    Sep 6, 2010
    Messages:
    29
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    software developer
    Location:
    india
    Thanks very much
     
  4. rahulonly4u

    rahulonly4u New Member

    Joined:
    Sep 6, 2010
    Messages:
    29
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    software developer
    Location:
    india
    Thanks again to LordN3mrod for this valuable tips i m going to do this mistake
     
  5. LordN3mrod

    LordN3mrod New Member

    Joined:
    Sep 4, 2010
    Messages:
    22
    Likes Received:
    11
    Trophy Points:
    0
    Occupation:
    Software Developer
    Location:
    Yerevan, Armenia
    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