Click here to Skip to main content
15,860,943 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
The macro to define a variable is:

//Define macro ============
#define mcr_defineVar(t,v)  t v;
//Use======================
      mcr_defineVar(int,a); //same as int a;
      a = 10; // =>a = 10;


But I want to do some statements:

mcr_defineVar("int",a);


OR

mcr_defineVar("int","a");


Expected is: int a;

I don't know to do it.

Can you help me, please?


Thank you!
TCBinh - VietNam
Posted
Updated 8-Dec-10 14:56pm
v3

Your code compiles fine in Visual C++ 6 (at least the highlighted code does). You won't need the double quotes that you use in your second example.
 
Share this answer
 
When you invoke the macro using:

mcr_defineVar("int",a);

the first parameter to the macros is the 5 characters: "int"

This parameter is substituted into the macro text (for t) verbatim. There is no way to avoid that. You can't get rid of the double-quotes.

However, the real question is why do you want to do this? As the previous answer says you don't need the double-quotes. I would also say that using a macro to define a variable is very confusing to anyone reading the code.

If you could tell us what you are trying to achieve, we could tell you the right way to do it.
 
Share this answer
 
Not sure if these will help

C#
// Helper macro BOOST_JOIN:
// The following piece of macro magic joins the two
// arguments together, even when one of the arguments is
// itself a macro (see 16.3.1 in C++ standard).  The key
// is that macro expansion of macro arguments does not
// occur in BOOST_DO_JOIN2 but does in BOOST_DO_JOIN.
//
#define BOOST_JOIN( X, Y ) BOOST_DO_JOIN( X, Y )
#define BOOST_DO_JOIN( X, Y ) BOOST_DO_JOIN2(X,Y)
#define BOOST_DO_JOIN2( X, Y ) X##Y
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900