By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 1,725 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

macro substitution

P: n/a
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::getDescription()
{
return desc;
}

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

Here come the problem :

string A::makeSQLString(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
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Johan wrote:
Can somebody help me with this problem : I have the following member
functions

#define FUNC(x) get##()
Didn't you mean

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

??

class A
{
the declarations etc, etc.
};

string A::getDescription()
{
return desc;
}

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

Here come the problem :

string A::makeSQLString(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


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::makeSQLString(const std::string& field)
{
std::ostringstream s;
s << "select * from customer where " << field << " = "
<< (field == "Number" ?
getNumber() : (field == "Description" ?
getDescription() : "NOTHING" ) );
return s.str();
}

Victor
Jul 22 '05 #2

P: n/a
On Fri, 29 Oct 2004 17:48:04 +0200 in comp.lang.c++,
"Johan" <me@knoware.nl> wrote,
string A::makeSQLString(string field)
{
stringstream s;

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


First off, is the rightmost << supposed to be a
operator<<(ostream&, std::string)
or
operator<<(ostream&, std::int)
I'd suggest something like:

s << "select * from customer where " << field << " = ";
if (field == "Description")
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 "preprocessor" because it does its stuff at a very early stage
in the compilation process.
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.