473,770 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Jul 22 '05 #1
2 1697
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::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


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
Jul 22 '05 #2
On Fri, 29 Oct 2004 17:48:04 +0200 in comp.lang.c++,
"Johan" <me@knoware.n l> wrote,
string A::makeSQLStrin g(string field)
{
stringstream s;

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


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.
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1438
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 made easy =--------- -= Get GrabIt for free from http://www.shemes.com/ =-
6
2254
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 that LOGMSG("Log Me\n"); will be a NO-OP if the Logger::Enabled() returns false, else the
8
5348
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
1627
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 bool indicating whether the field is a special field or not. The struct representing the value has a constructor that takes a single parameter for the length, and defaults the boolean to false.
7
23555
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. Execution is faster. Where will it be stotred?(Is it in bss/stack/?) FUNCTION:
14
2073
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 lot nicer to read: #define SET(x,y) (x |=(1<<y)) #define PIEZO PORTB,5
1
5441
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 ds2 = &a;
4
6926
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 of a type whose name is stored in a constant. For example: Const FORM_NAME_1 as String = "frmThisForm" Const FORM_NAME_2 as String = "frmThatForm"
3
2179
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
9005
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 inline function ? thanks for any help in advance ...
0
9617
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9453
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10099
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10036
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9904
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8929
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5481
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.