macro substitution

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Johan

    #1

    macro substitution

    Hi,

    Can somebody help me with this problem : I have the following member
    functions

    #define FUNC(x) get##()

    class A
    {
    the declarations etc, etc.
    };

    string A::getDescripti on()
    {
    return desc;
    }

    int A::getNumber()
    {
    return number;
    }

    Here come the problem :

    string A::makeSQLStrin g(string field)
    {
    stringstream s;

    s << "select * from customer where " << field << " = " << XXXX

    return s.str();
    }

    If want XXX to expand to getDescription or getNumber functions depending on
    the value of field. I tried it with a macro FUNC ( see above ) but that
    gives me FUNC(field) in the sql string. The func has to expanded to
    getNumber or getDescription function.

    How to do this

    John


  • Victor Bazarov

    #2
    Re: macro substitution

    Johan wrote:[color=blue]
    > Can somebody help me with this problem : I have the following member
    > functions
    >
    > #define FUNC(x) get##()[/color]

    Didn't you mean

    #define FUNC(x) get##x()

    ??
    [color=blue]
    >
    > class A
    > {
    > the declarations etc, etc.
    > };
    >
    > string A::getDescripti on()
    > {
    > return desc;
    > }
    >
    > int A::getNumber()
    > {
    > return number;
    > }
    >
    > Here come the problem :
    >
    > string A::makeSQLStrin g(string field)
    > {
    > stringstream s;
    >
    > s << "select * from customer where " << field << " = " << XXXX
    >
    > return s.str();
    > }
    >
    > If want XXX to expand to getDescription or getNumber functions depending on
    > the value of field. I tried it with a macro FUNC ( see above ) but that
    > gives me FUNC(field) in the sql string. The func has to expanded to
    > getNumber or getDescription function.
    >
    > How to do this[/color]

    There is no way. You're asking to expand a macro [at preprocessing-time]
    depending on the value of an object ('field') during run-time? That's
    done using 'if' statement.

    std::string A::makeSQLStrin g(const std::string& field)
    {
    std::ostringstr eam s;
    s << "select * from customer where " << field << " = "
    << (field == "Number" ?
    getNumber() : (field == "Descriptio n" ?
    getDescription( ) : "NOTHING" ) );
    return s.str();
    }

    Victor

    Comment

    • David Harmon

      #3
      Re: macro substitution

      On Fri, 29 Oct 2004 17:48:04 +0200 in comp.lang.c++,
      "Johan" <me@knoware.n l> wrote,[color=blue]
      >string A::makeSQLStrin g(string field)
      >{
      > stringstream s;
      >
      > s << "select * from customer where " << field << " = " << XXXX[/color]

      First off, is the rightmost << supposed to be a
      operator<<(ostr eam&, std::string)
      or
      operator<<(ostr eam&, std::int)


      I'd suggest something like:

      s << "select * from customer where " << field << " = ";
      if (field == "Descriptio n")
      s << getDescription( );
      else if (field == "Number")
      s << getNumber();

      You will certainly get little or no help from macros when you need to make
      a decision based on the run-time content of a string argument. It's
      called the "preprocess or" because it does its stuff at a very early stage
      in the compilation process.


      Comment

      Working...