473,395 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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::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
2 1676
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Les | last post by:
Is there any way to do something like macro substitution in VB, like in C++ or C or other languages? --------------= Posted using GrabIt =---------------- ------= Binary Usenet downloading...
6
by: Raghuveer Pallikonda | last post by:
Hi, I am trying to stub out debug log messages in my application, if the logging subsystem is not enabled.. For e.g a invocation #define LOGMSG !Logger::Enabled() ? false : Logger::LogMsg so...
8
by: Siemel Naran | last post by:
#define EXPECT_ASSERT(x) { if (!x) expect_assert(localVariable, __FILE__, __LINE__, #x); } MSVC7 gives an error: "error C2014: preprocessor command must start as first nonwhite space".
1
by: me | last post by:
Hi guys I want to insert a load of pieces of data into a map The map has an std::string representing a field name as the key, and the value is a struct with 2 members - the field length and a...
7
by: Newbie_sw2003 | last post by:
Where should I use them? I am giving you my understandings. Please correct me if I am wrong: MACRO: e.g.:#define ref-name 99 The code is substituted by the MACRO ref-name. So no overhead....
14
by: Malcolm | last post by:
Hi, I have the following which fails with "disagreement in number of macro arguments" when compiling with Imagecraft ICCAVR. Has anyone got any ideas - its not vital but would make the code a...
1
by: Rodolfo | last post by:
Hello, there's another languages that can do a macro substitution, how can I do this in Csharp. This is an example of what I want to do Dataset ds = new Dataset; string a = "ds"; DataSet...
4
by: Don | last post by:
I think "macro substitution" is the correct term for what I want to do, but, to be sure, here is a description of what I'd like to know is possible: I want to be able to create a create an object...
3
by: XHengDF | last post by:
recent days. i am confuse on the macros in boost. could anyone tell me the replacement rules of the macros in c++, or somelinks about the macros in boost thanks
37
by: junky_fellow | last post by:
hi guys, Can you please suggest that in what cases should a macro be preferred over inline function and viceversa ? Is there any case where using a macro will be more efficient as compared to...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.