macro substitution

J

Johan

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
 
V

Victor Bazarov

Johan said:
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::eek:stringstream s;
s << "select * from customer where " << field << " = "
<< (field == "Number" ?
getNumber() : (field == "Description" ?
getDescription() : "NOTHING" ) );
return s.str();
}

Victor
 
D

David Harmon

On Fri, 29 Oct 2004 17:48:04 +0200 in comp.lang.c++,
Johan said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top