473,386 Members | 1,647 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,386 software developers and data experts.

macro

38
hi all,

look at this function:
Expand|Select|Wrap|Line Numbers
  1. string test(string str)}{
  2.  
  3. str.insert("my string");
  4.  
  5. return str;
  6. }
  7.  
  8.  
how can i implement it as a macro.

thank for each support
Sep 12 '09 #1
11 1652
Tassos Souris
152 100+
A macro replaces a text for some other text. The text that is to be replaced is
test and the replacement text is str.insert( "my string" ).
Expand|Select|Wrap|Line Numbers
  1. #define text_to_be_replaced( parameter ) replacement_text
Sep 12 '09 #2
mar11
38
i do't know it dose not work with me...

Expand|Select|Wrap|Line Numbers
  1. #include <string>
  2. #include <iostream>
  3.  
  4.  string TEST1(string str){
  5.  
  6.  str.append("my string");
  7.  
  8.  return str;
  9.  }
  10.  
  11. int main(){
  12. string stri;
  13. stri = TEST1("this string is ");  cout << stri<<endl;
  14. }  // output -- this string is my string
  15.  
Now, how can i implement this function as macro and how will be returned..

please can you explain it through executable example

thanks for each support
Sep 12 '09 #3
donbock
2,426 Expert 2GB
What have you got so far for the macro?

By the way, as an avid C programmer I'm very big on macros ... but I've heard rumblings that C++ experts strongly advise against using them. If this is a self-assigned task so you have flexibility, your C++ skills might be better served by creating an inline function.
Sep 12 '09 #4
Tassos Souris
152 100+
Assume this code example:
Expand|Select|Wrap|Line Numbers
  1. #define MAX_STRING_LEN 100
  2.  
  3. char string[ MAX_STRING_LEN ];
  4.  
We define MAX_STRING_LEN to be 100. This means that whenever the
text MAX_STRING_LEN is found it is replaced by 100. So, after the preprocessor step, the above code now becomes:
Expand|Select|Wrap|Line Numbers
  1. char string[ 100 ];
  2.  
Now let's take a macro that takes an integer and increments it:
Expand|Select|Wrap|Line Numbers
  1. #define INT_INCR( integer ) ( ( integer )++ )
  2.  
  3. int x = 0;
  4. INT_INCR( x );
  5.  
After the preprocessor step the above code becomes:
Expand|Select|Wrap|Line Numbers
  1. int x = 0;
  2. (x)++;
  3.  
Hope that helps. Show us what you have done so far.
Sep 13 '09 #5
mar11
38
Dear Tassos,

my main question is still not answerd to tell you what i have done.. So i realy confused about the bossibility of implementing the returned functions (like the example i have written) as a macro. I know i can implement it as inline functions and inline functions is better to interpret form compiler. but in many applications the macros are still used ...

could you please focuse on this point?

thank for each support...
Sep 13 '09 #6
donbock
2,426 Expert 2GB
mar11: Tassos and I are gently urging you to post your best effort first. We'll try to guide you from there.
Sep 13 '09 #7
mar11
38
Also, this macro will be work okay
Expand|Select|Wrap|Line Numbers
  1. #define TEST( a, b )  (a = b+ 4)
  2.  
  3.  
  4. int c(0), d(0), e(0);
  5. e = TEST( c,d);
  6. cout << e << endl;
  7.  
  8.  
but when i want to add a for statement, it does not work anymore??

is there any bossibility to add for statment in the macro??

thank a lot in advance
Expand|Select|Wrap|Line Numbers
  1. #define TEST( a, b ) for(int i=0; i<10; i++) (a = b+ i)
  2.  
  3.  
  4. int c(0), d(0), e(0);
  5. e = TEST( c,d);
  6. cout << e << endl;
  7.  
  8.  
Sep 13 '09 #8
donbock
2,426 Expert 2GB
@mar11
This macro ought to expand to a proper for-loop. The problem is that you're trying to assign the value of the for-loop to e. For-loops don't have a value. You macro expands to this:
Expand|Select|Wrap|Line Numbers
  1. e = for(int i=0; i<10; i++) (c = d+i);
Sep 13 '09 #9
mar11
38
thanks, it works now..
Sep 13 '09 #10
weaknessforcats
9,208 Expert Mod 8TB
Tassos: Do not use macros in C++.

1) you have no control over how the user exands the macro. You can get results that are inaccurate or just won't compile.
2) when you debug the code for the macro is not in your source file so your debugger cannot step through the macro.
3) that forces you to stop the build after the preprocessopr step and visually examine the macro expansion to see that it expanded to something that makes sense
4) etc...

Macros are really bad news.

Your example in C++ is solved by using a template.
Sep 13 '09 #11
Tassos Souris
152 100+
@weaknessforcats
No i am not using C++.... :)
And yes i agree with you... :)

Personally even in C (i use C not C++) i prefer enumerations over #defines and when it comes to macros with parameters i always provide a function version.
Sep 13 '09 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

25
by: Andrew Dalke | last post by:
Here's a proposed Q&A for the FAQ based on a couple recent threads. Appropriate comments appreciated X.Y: Why doesn't Python have macros like in Lisp or Scheme? Before answering that, a...
699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
2
by: Pete | last post by:
In Access 95/97 I used to be able to create pull down menus (File,Edit ...) from a macro. It seems there used to be some wizard for that. However in Access 2000 it seems you have to build your...
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....
3
by: Alexander Ulyanov | last post by:
Hi all. Is it possible to pass the whole blocks of code (possibly including " and ,) as macro parameters? I want to do something like: MACRO(FOO, "Foo", "return "Foobar";", "foo();...
8
by: lasek | last post by:
Hi...in some posts i've read...something about using macro rather then function...but difference ??. Best regards....
12
by: Laurent Deniau | last post by:
I was playing a bit with the preprocessor of gcc (4.1.1). The following macros expand to: #define A(...) __VA_ARGS__ #define B(x,...) __VA_ARGS__ A() -nothing, *no warning* A(x) -x ...
6
by: Takeadoe | last post by:
Dear NG, Can someone assist me with writing the little code that is needed to run an update table query each time the database is opened? From what I've been able to glean from this group, the...
5
by: Bill | last post by:
This database has no forms. I am viewing an Access table in datasheet view. I'd like to execute a macro to execute a function (using "runcode"). In the function, I'll reading data from the record...
0
by: =?Utf-8?B?TGV0emRvXzF0?= | last post by:
I'd like to create a Macro that will sort some raw data, apprx 20k lines, remove some lines based upon a condition in a certain column. Then copy this data into a new spreadsheet and sort the ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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,...

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.